0

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 用来移动图像。移动后如何固定它的位置。我的意思是当我选择正确的位置时,图像不应该再移动了。

4

2 回答 2

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.
}
于 2013-11-13T08:34:20.713 回答
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);
          } 
    }
}  
于 2013-11-13T08:11:04.767 回答