iOS 如何使用 touchesMoved 移动两个图像?
Posted
技术标签:
【中文标题】iOS 如何使用 touchesMoved 移动两个图像?【英文标题】:iOS How to use touchesMoved to move two images? 【发布时间】:2012-07-02 20:07:01 【问题描述】:我有两个不同的UIImage
s。
我想允许用户将这些图像单独移动到UIView
上。
我怎么能这样做?
IBOutlet UIImageView *image01;
IBOutlet UIImageView *image02;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:touch.view];
【问题讨论】:
能否在定义CGPoint location =
之后添加这一行:imageToBeMoved.center = location
并查看它是否有效。
【参考方案1】:
我不确定我是否理解这个问题。您可以使用标准平移手势识别器,每个UIImageView
一个,例如:
UIPanGestureRecognizer *pan1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanImage:)];
image01.userInteractionEnabled = YES;
[self.image01 addGestureRecognizer:pan1];
UIPanGestureRecognizer *pan2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanImage:)];
image02.userInteractionEnabled = YES;
[self.image02 addGestureRecognizer:pan2];
和
- (void)handlePanImage:(UIPanGestureRecognizer *)sender
static CGPoint originalCenter;
if (sender.state == UIGestureRecognizerStateBegan)
originalCenter = sender.view.center;
sender.view.alpha = 0.8;
[sender.view.superview bringSubviewToFront:sender.view];
else if (sender.state == UIGestureRecognizerStateChanged)
CGPoint translation = [sender translationInView:self.view];
sender.view.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y);
else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateFailed)
// do whatever post dragging you want, e.g.
// snap the piece into place
[UIView animateWithDuration:0.2 animations:^
CGPoint center = sender.view.center;
center.x = round(center.x / 50.0) * 50.0;
center.y = round(center.y / 50.0) * 50.0;
sender.view.center = center;
sender.view.alpha = 1.0;
];
或者您可以在他们的超级视图上创建一个手势识别器,然后使用 CGRectContainsPoint
来查看您的起始 locationInView
是否在您的两个 UIImageView 之一的框架内,然后继续为适当的一个设置动画。
例如,在您的视图控制器中,您可以创建一个手势识别器:
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanSuper:)];
[self.view addGestureRecognizer:pan];
并且处理程序可能看起来像(如果需要,您也可以将其实现为自定义手势识别器):
- (void)handlePanSuper:(UIPanGestureRecognizer *)sender
static UIImageView *viewToMove;
static CGPoint originalCenter;
if (sender.state == UIGestureRecognizerStateBegan)
CGPoint location = [sender locationInView:self.view];
if (CGRectContainsPoint(self.image01.frame, location))
viewToMove = image01;
originalCenter = viewToMove.center;
else if (CGRectContainsPoint(self.image02.frame, location))
viewToMove = image02;
originalCenter = viewToMove.center;
else
viewToMove = nil;
if (viewToMove)
viewToMove.alpha = 0.8;
[viewToMove.superview bringSubviewToFront:viewToMove];
else if (sender.state == UIGestureRecognizerStateChanged && viewToMove != nil)
CGPoint translation = [sender translationInView:self.view];
viewToMove.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y);
else if ((sender.state == UIGestureRecognizerStateEnded ||
sender.state == UIGestureRecognizerStateFailed ||
sender.state == UIGestureRecognizerStateCancelled) && viewToMove != nil)
// do whatever post dragging you want, e.g.
// snap the piece into place
[UIView animateWithDuration:0.2 animations:^
CGPoint center = viewToMove.center;
center.x = round(center.x / 50.0) * 50.0;
center.y = round(center.y / 50.0) * 50.0;
viewToMove.center = center;
viewToMove.alpha = 1.0;
];
viewToMove = nil;
如果我误解了你的问题,请告诉我。
【讨论】:
没关系,但我正在为孩子们开发一款益智游戏,让他们一起收集图像碎片。对不起我的英语。 @YaserAlNamshan 没问题。令我震惊的是,上述任何一种技术都应该可以正常工作。如果您不想这样做,则无需对识别器进行子类化和使用。唯一的问题是您是想要在每张图像上使用手势识别器,还是只需要一个手势识别器。顺便说一句,我已经用各种增强的示例更新了我的代码示例,例如拖动时减少 alpha,移动到前面以免被其他图像遮挡,最后将片段对齐等。只是给你一些想法。 谢谢罗伯特,我想我会使用这种技术:)以上是关于iOS 如何使用 touchesMoved 移动两个图像?的主要内容,如果未能解决你的问题,请参考以下文章
在 touchesMoved 函数中移动 2 个不同的 Spritenodes
UIScrollview touchesbegin,touchesmoved,touchesendactions
在滚动视图中的自定义视图中使用触摸进行缩放。 touchesMoved 未调用