0

画像を移動するために使用- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event しています。移動後にその位置を修正する方法を教えてください。つまり、正しい位置を選択すると、画像が動かなくなります。

4

2 に答える 2

3

画像を含む imageView に PanGesture を追加します。

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 に答える