无需触摸图像即可拖动 UIImageView? [关闭]
Posted
技术标签:
【中文标题】无需触摸图像即可拖动 UIImageView? [关闭]【英文标题】:Dragging a UIImageView without having to touch the Image? [closed] 【发布时间】:2014-04-03 19:48:05 【问题描述】:我的应用有一个 UIView,其中有一个 UIImageView。用户需要能够移动该图像,但不一定要触摸该图像。
【问题讨论】:
【参考方案1】:只要声明
NSInteger offsetX;
NSInteger offsetY;
并在您的视图控制器中实现以下方法:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//Get touch coordinates in location view
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
//Calculate offset on touches began
offsetX = touchPoint.x - imageView.frame.origin.x;
offsetY = touchPoint.y - imageView.frame.origin.y;
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
//Get touch coordinates in location view
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
//Create new frame with origin offset
CGRect newFrame = CGRectMake(touchPoint.x - offsetX, touchPoint.y - offsetY, imageView.bounds.size.width, imageView.bounds.size.height);
//Set the new frame
imageView.frame = newFrame;
【讨论】:
正是我想要的!非常感谢您的快速回答!以上是关于无需触摸图像即可拖动 UIImageView? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章