iOS阶段学习第35天笔记(Touch手势介绍)

一、Touch手势

1、利用手势实现UIButton移动效果  实例代码

1) 创建一个继承自UIButton的类 MyButton.h  代码实现

 #import <UIKit/UIKit.h>
@interface MyButton : UIButton
@end

2)MyButton.m  的代码实现

 #import "MyButton.h"
@implementation MyButton
{
CGPoint _lastPoint;
} //手势开始
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSLog(@"began:%@",NSStringFromCGPoint(point));
_lastPoint = point;
} -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
CGFloat offsetx = point.x - _lastPoint.x;
CGFloat offsety = point.y - _lastPoint.y;
self.center = CGPointMake(self.center.x + offsetx, self.center.y + offsety);
NSLog(@"moved:%@",NSStringFromCGPoint(point));
} -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSLog(@"end:%@",NSStringFromCGPoint(point));
}
@end

3)父视图中的代码实现

 #import "ViewController.h"
#import "MyButton.h"
@interface ViewController ()
{
MyButton *_v;
CGPoint _lastPoint;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
_v = [[MyButton alloc]initWithFrame:CGRectMake(, , , )];
_v.backgroundColor = [UIColor redColor];
[self.view addSubview:_v];
} //手势开始
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
NSLog(@"began:%@",NSStringFromCGPoint(point));
_lastPoint = point;
} -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
CGFloat offsetx = point.x - _lastPoint.x;
CGFloat offsety = point.y - _lastPoint.y;
_v.center = CGPointMake(_v.center.x + offsetx, _v.center.y + offsety);
_lastPoint = point;
NSLog(@"moved:%@",NSStringFromCGPoint(point));
} -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
NSLog(@"end:%@",NSStringFromCGPoint(point));
} -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{ }
@end

2、利用Touch手势实现控件的缩放与旋转效果 实例代码

 #import "ViewController.h"
//遵守旋转与缩放的代理协议
@interface ViewController ()<UIGestureRecognizerDelegate>
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];
imgv.center = self.view.center;
[self.view addSubview:imgv];
imgv.image = [UIImage imageNamed:@""];
imgv.userInteractionEnabled = YES; //点击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGes:)]; //设置该手势需要的手指数
tap.numberOfTouchesRequired = ; //设置该手势的点击次数
tap.numberOfTapsRequired = ;
[imgv addGestureRecognizer:tap]; //平移手势,拖拽手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)];
[imgv addGestureRecognizer:pan]; //缩放手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGes:)];
[imgv addGestureRecognizer:pinch];
pinch.delegate = self; //旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGes:)];
[imgv addGestureRecognizer:rotation];
rotation.delegate = self;
} //返回值表示能否同时识别其他(相对于已经设置了代理的手势)手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
-(void)rotationGes:(UIRotationGestureRecognizer *)rotation
{
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
rotation.rotation = 0.0;
}
-(void)pinchGes:(UIPinchGestureRecognizer *)pinch
{
//transform:仿射变换
//pinch.scale,是缩放手势的捏合倍率
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale); //倍率还原
pinch.scale = 1.0;
}
-(void)panGes:(UIPanGestureRecognizer *)pan
{
//返回当前的手势的偏移量
CGPoint offset = [pan translationInView:pan.view];
//pan.view就是pan手势所加到的视图
pan.view.center = CGPointMake(pan.view.center.x + offset.x, pan.view.center.y + offset.y);
//移动以后,把偏移量归0
[pan setTranslation:CGPointZero inView:pan.view];
} -(void)tapGes:(UIGestureRecognizer *)tap
{
NSLog(@"==========");
}
@end
 
 
 
上一篇:React Native之配置URL Scheme(iOS Android)


下一篇:PHP、Java、Python、C、C++ 这几种编程语言都各有什么特点或优点