我正在尝试为我的imageView(下面的代码中的maskPreview)创建移动功能,以便用户可以在屏幕上移动一张包含在maskPreview中的图片。这里是我的代码触摸开始和触摸移动:如何用触摸移动UIImageView
-(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)];
}
}
但我从应用程序得到一些奇怪的反应。我没有限制imageview可以移动多远,也就是说,为了防止它离开屏幕,但即使这只是一个小动作,我的图像视图也会变得疯狂并消失。
非常感谢提前对所有帮助
太棒了马特,你是一个绝对的天才!会评价你的答案,但我没有足够的声望!让我的生活变得如此轻松! – Septronic 2012-02-03 13:47:23
只是仅供参考,代码直接出我的书:http://shop.oreilly.com/product/0636920023562.do - 更多的地方来自... – matt 2012-02-03 14:31:21
真棒,我会研究它:)谢谢 – Septronic 2012-02-03 14:47:50