失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > IOS 学习---触摸事件与手势

IOS 学习---触摸事件与手势

时间:2018-12-28 07:21:29

相关推荐

IOS 学习---触摸事件与手势

独角兽企业重金招聘Python工程师标准>>>

单击手势与双击手势

//单击手势UITapGestureRecognizer*tap1=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(singleTapAction:)];//设置点击的次数tap1.numberOfTapsRequired=1;//设置手指的个数tap1.numberOfTouchesRequired=1;//将手势添加到视图上[self.viewaddGestureRecognizer:tap1];//双击手势UITapGestureRecognizer*tap2=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(doubleAction:)];tap2.numberOfTapsRequired=2;//添加手势[self.viewaddGestureRecognizer:tap2];//区别两个手势[tap1requireGestureRecognizerToFail:tap2];}//单击手势响应的事件-(void)singleTapAction:(UITapGestureRecognizer*)tap{//关闭手势//tap.enabled=NO;//获取手势所在的视图UIView*view=tap.view;view.backgroundColor=[UIColorcolorWithRed:arc4random_uniform(10)/10.0green:arc4random_uniform(10)/10.0blue:arc4random_uniform(10)/10.0alpha:1];NSLog(@"singleTapAction");}//双击手势响应的事件-(void)doubleAction:(UITapGestureRecognizer*)tap{NSLog(@"doubleAction");}

轻扫手势

-(void)_initSwipeGesture{UISwipeGestureRecognizer*swipeGesture=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipeAction:)];//设置轻扫的方向/*UISwipeGestureRecognizerDirectionRightUISwipeGestureRecognizerDirectionLeftUISwipeGestureRecognizerDirectionUpUISwipeGestureRecognizerDirectionDown*/swipeGesture.direction=UISwipeGestureRecognizerDirectionDown;[self.viewaddGestureRecognizer:swipeGesture];}-(void)swipeAction:(UISwipeGestureRecognizer*)swipe{NSLog(@"swipeAction");}

平移手势

-(void)_initPanGesture{UIPanGestureRecognizer*panGesture=[[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panAction:)];[self.viewaddGestureRecognizer:panGesture];}-(void)panAction:(UIPanGestureRecognizer*)pan{//手指所在的坐标CGPointpoint=[panlocationInView:self.view];_view.center=point;}

长按手势

-(void)_initLongGesture{UILongPressGestureRecognizer*pressGesture=[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(pressAction:)];//设置最短时间pressGesture.minimumPressDuration=1;[self.viewaddGestureRecognizer:pressGesture];}-(void)pressAction:(UILongPressGestureRecognizer*)press{/*UIGestureRecognizerStateBegan开始UIGestureRecognizerStateChanged改变UIGestureRecognizerStateEnded结束UIGestureRecognizerStateCancelled取消*/if(press.state==UIGestureRecognizerStateBegan){NSLog(@"pressAction");}}

旋转手势

-(void)_initRotationGesture{UIImageView*imgView=[[UIImageViewalloc]initWithFrame:[UIScreenmainScreen].bounds];imgView.userInteractionEnabled=YES;imgView.image=[UIImageimageNamed:@"5.jpeg"];[self.viewaddSubview:imgView];UIRotationGestureRecognizer*rotationGresutre=[[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotationAction:)];[imgViewaddGestureRecognizer:rotationGresutre];}-(void)rotationAction:(UIRotationGestureRecognizer*)rotationGesture{NSLog(@"%f",rotationGesture.rotation);UIImageView*imgView=(UIImageView*)rotationGesture.view;if(rotationGesture.state==UIGestureRecognizerStateChanged){imgView.transform=CGAffineTransformMakeRotation(rotationGesture.rotation);}elseif(rotationGesture.state==UIGestureRecognizerStateEnded||rotationGesture.state==UIGestureRecognizerStateCancelled){[UIViewanimateWithDuration:0.3animations:^{imgView.transform=CGAffineTransformIdentity;}];}}

缩放手势

-(void)_initPinchGesture{UIImageView*imgView=[[UIImageViewalloc]initWithFrame:[UIScreenmainScreen].bounds];imgView.userInteractionEnabled=YES;imgView.image=[UIImageimageNamed:@"5.jpeg"];[self.viewaddSubview:imgView];UIPinchGestureRecognizer*pinchGesture=[[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchAction:)];[imgViewaddGestureRecognizer:pinchGesture];}-(void)pinchAction:(UIPinchGestureRecognizer*)pinch{//pinch.scale;//缩放比NSLog(@"pinchAction:%f",pinch.scale);UIImageView*imgView=(UIImageView*)pinch.view;if(pinch.state==UIGestureRecognizerStateChanged){imgView.transform=CGAffineTransformMakeScale(pinch.scale,pinch.scale);}elseif(pinch.state==UIGestureRecognizerStateEnded||pinch.state==UIGestureRecognizerStateCancelled){[UIViewanimateWithDuration:0.3animations:^{imgView.transform=CGAffineTransformIdentity;}];}}

如果觉得《IOS 学习---触摸事件与手势》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。