我正在尝试为我的imageView(下面的代码中的maskPreview)创建移动功能,以便用户可以在屏幕周围移动maskPreview中包含的图片.这是触摸开始和触摸移动的代码:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count]==1) {
UITouch *touch= [touches anyObject];
originalOrigin = [touch locationInView:maskPreview];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count]==1) {
UITouch *touch = [touches anyObject];
CGPoint lastTouch = [touch previousLocationInView:self.view];
CGFloat movedDistanceX = originalOrigin.x-lastTouch.x;
CGFloat movedDistanceY = originalOrigin.y-lastTouch.y;
[maskPreview setFrame:CGRectMake(maskPreview.frame.origin.x+movedDistanceX, maskPreview.frame.origin.y + movedDistanceY, maskPreview.frame.size.width, maskPreview.frame.size.height)];
}
}
Run Code Online (Sandbox Code Playgroud)
但我从应用程序得到一些奇怪的回应.我没有限制图像视图可以移动多远,即防止它移出屏幕,但即使这是一个小动作,我的图像视图也会疯狂消失.
非常感谢所有的帮助