Wednesday, January 16, 2013

moving Image in UIView using UIPanGesture


- (void)viewDidLoad{

    YOURIMAGEVIEW = [[UIImageView alloc]initWithFrame:CGRectMake(130350100100)];
    YOURIMAGEVIEW.image = [UIImage imageNamed:@"image.png"];
    [self.view addSubview: YOURIMAGEVIEW];
    YOURIMAGEVIEW.userInteractionEnabled = YES;
    [self.view bringSubviewToFront: YOURIMAGEVIEW];
  
    panRecognizer = [[UIPanGestureRecognizer allocinitWithTarget:self action:@selector(handlePanFrom:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [self. YOURIMAGEVIEW addGestureRecognizer:panRecognizer];    

}


- (void)handlePanFrom:(UIPanGestureRecognizer*)recognizer {
    
    CGPoint translation = [recognizer translationInView:recognizer.view];
    CGPoint velocity = [recognizer velocityInView:recognizer.view];
    
    if (recognizer.state == UIGestureRecognizerStateBegan) {
    } else if (recognizer.state == UIGestureRecognizerStateChanged) {
      
    } else if (recognizer.state == UIGestureRecognizerStateEnded) {
       // <animate to final position>
        [UIView animateWithDuration:2.0 delay:0  options:UIViewAnimationOptionCurveEaseOut   animations:^ { self.YOURIMAGEVIEW.center = CGPointMake(velocity.x, velocity.y);} completion:NULL];

    }
}
}

No comments:

Post a Comment