Ios 移动图像后如何确定图像的位置?

Ios 移动图像后如何确定图像的位置?,ios,objective-c,uitouch,Ios,Objective C,Uitouch,我正在使用-(void)touchsmoved:(NSSet*)touchs with event:(UIEvent*)event移动图像。如何在移动图像后固定其位置。我的意思是,我希望当我选择正确的位置时,图像不应再移动。您可以这样做: -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; UIImageView

我正在使用
-(void)touchsmoved:(NSSet*)touchs with event:(UIEvent*)event
移动图像。如何在移动图像后固定其位置。我的意思是,我希望当我选择正确的位置时,图像不应再移动。

您可以这样做:

-(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);
          } 
    }
}  
您可以这样做:

-(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);
          } 
    }
}  

将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.
}

将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.
}

你试过什么?展示一些你是如何尝试的代码。无论如何,我会为图像存储一个bool值——万一触摸移动到某个右侧或另一个视图上方,我会检测到它,并更改bool值——作为不再移动的图像。然后在触摸移动后,我会检查bool值,如果这是真的,我会返回。您尝试了什么?展示一些你是如何尝试的代码。无论如何,我会为图像存储一个bool值——万一触摸移动到某个右侧或另一个视图上方,我会检测到它,并更改bool值——作为不再移动的图像。然后在触摸移动后,我会检查bool值,如果这是真的,则返回。