首页 > 学院 > 开发设计 > 正文

触摸事件-拖动view

2019-11-14 19:38:05
字体:
来源:转载
供稿:网友

案例:通过触摸事件拖动imageView和view1和view2

注意:1.imgeview默认不能响应触摸事件,2.视图有三个子视图,如何区分多个视图

用storyBoard托人2个view和一个imageView,IBOutlet连线

案例图片:

//让imageView接受用户触摸

1 [self.imageView setUserInteractionEnabled:YES];

区分哪个视图

1 [touch view] == self.imageView

 

触摸事件代码

 

 1 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 2 { 3     //1.获取用户点击 4     UITouch *touch = [touches anyObject]; 5     CGPoint location = [touch locationInView:self.view]; 6     CGPoint PReLocation = [touch previousLocationInView:self.view]; 7     CGPoint dertPoint = CGPointMake(location.x - preLocation.x, location.y - preLocation.y); 8     //2.判断点击了那个视图 9     if ([touch view] == self.imageView) {10         NSLog(@"点击了图像");11         12         [self.imageView setCenter:CGPointMake(self.imageView.center.x + dertPoint.x, self.imageView.center.y + dertPoint.y)];13     }else if ([touch view] == self.redView){14         NSLog(@"点击了 hongse");15         [self.redView setCenter:CGPointMake(self.redView.center.x + dertPoint.x, self.redView.center.y + dertPoint.y)];16         17     }else if ([touch view] == self.greenView){18         //提示最好不要直接用else处理,因为随时有可能添加新的控件19         NSLog(@"点击 green");20         [self.greenView setCenter:CGPointMake(self.greenView.center.x + dertPoint.x, self.greenView.center.y + dertPoint.y)];21     }22     23 }

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表