我正在使用- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
来移动图像。如何移动它后我可以修复它的位置。我的意思是,当我选择正确的位置时,图像不应再移动。
答案 0 :(得分:3)
将PanGesture添加到包含图像的imageView。
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
[imageView addGestureRecognizer:panRecognizer];
//method to move imageView whereever you want in the view,you can also write conditions to restrict the imageView not to go beyond bounds.
- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
{
CGPoint translation = [panRecognizer translationInView:self.view];
CGPoint imageViewPosition = imageView.center;
imageViewPosition.x += translation.x;
imageViewPosition.y += translation.y;
imageView.center = imageViewPosition;
[panRecognizer setTranslation:CGPointZero inView:self.view];
//you can use if(panRecognizer.state==UIGestureRecognizerStateEnded){} condition to fix the position of the imageView.
}
答案 1 :(得分:1)
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
UIImageView *view1 =moveImageView;
CGPoint point = [touch locationInView:self.view];
//the right position
if(point.x==?&&point.y==?){
rightPosition = YES;
moveImageView.frame = CGRectMake(point.x, point.y, 50, 50);
}else{
//the UIImageView you want to move
if(!rightPosition){
moveImageView.frame = CGRectMake(point.x, point.y, 50, 50);
}
}
}