UI中的手势

手势



一.触摸类UITouch

通过触摸类,我们可以实现各式的自定义手势

(1)创建UITouch

TouchView *view=[[TouchView alloc] initWithFrame:CGRectMake(50, 100, 200, 200)];

(2)开始触摸

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

NSLog(@"开始触摸");

①单点触摸

    UITouch *touch=[touches anyObject];

    CGPoint point=[touch locationInView:self.view];

    NSLog(@"%@",NSStringFromCGPoint(point));

②多点触摸

    NSSet *touchesSet=[event allTouches];

    for (UITouch *touch in touchesSet) {

        CGPoint point=[touch locationInView:self.view];

        NSLog(@"%@",NSStringFromCGPoint(point));


    }

#endif

}

(3)触摸结束

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSLog(@"触摸结束");

}

(4)触摸移动

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSLog(@"触摸移动");

}

(5)来电中断

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSLog(@"来电中断");

}



二.手势类 UIGestureRecognizer

UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

获取图片方法1:这种获取图片的方法不会把图片放到内存了,但只会复制一份

NSString *path=[[NSBundle mainBundle] pathForResource:@"10_0" ofType:@"jpg"];

NSData *data=[NSData dataWithContentsOfFile:path];

UIImage *image=[UIImage imageWithData:data];

方法2:正常获取图片的方法

imageView.image=[UIImage imageNamed:@"10_0"];

imageView.image=image;

[self.view addSubview:imageView];

[imageView release];

1、点击手势UITapGestureRecognizer

(1)①添加敲击手势

UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];

②开启

imageView.userInteractionEnabled=YES;

[imageView addGestureRecognizer:tap];

[tap release];


(2)①添加双击手势

一个视图可以有多个手势,但是一个手势只能添加到一个视图上

UITapGestureRecognizer *doubelTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapClick:)];

②设置点击几次触发事件(这里是两次,也就是双击)

doubelTap.numberOfTapsRequired=2;

[imageView addGestureRecognizer:doubelTap];

[doubelTap release];


(3)优先响应双击手势方法,如果双击失败,再响应单击手势

[tap requireGestureRecognizerToFail:doubelTap];



2、长按手势UILongPressGestureRecognizer

imageView.userInteractionEnabled=YES;

(1)添加长按手势

UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];

(2)设置几根手指有效

longPress.numberOfTapsRequired=1;

(3)设置最小的响应时间

longPress.minimumPressDuration=2;




3、滑动手势UISwipeGestureRecognizer

imageView.userInteractionEnabled=YES;

(1)添加滑动手势sw

UISwipeGestureRecognizer *leftSwipe=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];

(2)设置滑动方向

leftSwipe.direction=UISwipeGestureRecognizerDirectionLeft;

UISwipeGestureRecognizer *rightSwipe=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];

rightSwipe.direction=UISwipeGestureRecognizerDirectionRight;

(3)将滑动手势添加到视图上

[imageView addGestureRecognizer:leftSwipe];

[imageView addGestureRecognizer:rightSwipe];




三.手势的四种常用状态

1、刚开始生效UIGestureRecognizerStateBegan,

2、每次移动UIGestureRecognizerStateChanged,

3、手势结束UIGestureRecognizerStateEnded, 

4、手势被取消UIGestureRecognizerStateCancelled,



四、其他手势

1、拖动手势UIPanGestureRecognizer

imageView.userInteractionEnabled=YES;

UIPanGestureRecognizer *pgr=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panClick:)];




2、旋转手势UIRotationGestureRecognizer

imageView.userInteractionEnabled=YES;

UIRotationGestureRecognizer *rgr=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];




3、捏合手势UIPinchGestureRecognizer

imageView.userInteractionEnabled=YES;

UIPinchGestureRecognizer *pgr=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];




五、同时使用两种手势

利用代理UIGestureRecognizerDelegate


实现后结果:




六.视图摇摆

layer类似于ps的图层,如果把一个UIView看做图片的话,layer就像是图层.一个图片是由很多个大小不同的有层次的图层构成的, UIView也是.

1、添加摇动动画(基本动画)

CABasicAnimation * shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

2、设置时间

shakeAnimation.duration=duration;

shakeAnimation.fromValue=[NSNumber numberWithFloat:-raid];

shakeAnimation.toValue=[NSNumber numberWithFloat:raid];

3、自动倒带设置

shakeAnimation.autoreverses=YES;

4、完成移除

shakeAnimation.removedOnCompletion=YES;

5、设置重复次数,FLT_MAX为最大次数

if (count==0) {

shakeAnimation.repeatCount=FLT_MAX;

}else{

shakeAnimation.repeatCount=count;

}

6、添加给视图 layer用于设置动画,修改视图圆角等

[self.layer addAnimation:shakeAnimation forKey:@"Let me shake"];

7、停止视图摇摆

[self.layer removeAnimationForKey:@"Let me shake"];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值