touch事件用法:让view随手指移动

UIGestrure更适合单独的交互,touch事件才适合精细且动态的交互,记录下写法:

首先我的目的是移动一个UIImageview,让它随着我的手指移动,代码如下:

touch开始

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

{

UITouch *touch = [touches anyObject];

if (touch.view == backgroundView) {     //判断是否backgroundView接受touch事件

NSLog(@”touch begin”);

}

}

让UIImageview随手指移动的touchmove事件

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

{

UITouch *touch = [touches anyObject];

if (touch.view == backgroundView) {

CGPoint point = [touch locationInView:backgroundView];//获取touch坐标

point.x=pointy<0? 0:point.x;

point.x=pointy>100? 100:point.x;

point.y=pointy<0? 0:point.y;

point.y=pointy>100? 100:point.y;//限定手指一动获取的坐标在100*100的举行范围内

CGRect rect = myImageView.frame;

rect.origin.x =point.x;

rect.origin.y =point.y;

myImageView.frame = rect;

}

 

1 评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注