Search code examples
objective-ccocoa-touchuiimageviewcore-animationuitapgesturerecognizer

Translation animation being ignored until second tap


I have the following code that runs whenever a tap gesture is recognised. It moves a UIImageView to the tap location and also rotates the UIImageView.

However, the translation animation never runs on the first tap. What am I doing wrong?

-(void) onTap:(UITapGestureRecognizer *)tap{

    CGPoint point = [tap locationInView:self.view];



    [UIView animateWithDuration:1.2
                          delay:0
                        options:0
                     animations:^{
                         self.icon.center = point;
                     } completion:^(BOOL finished) {
                         //
                     }];

    [UIView animateWithDuration:1.2
                          delay:0
                        options:0
                     animations:^{
                         self.icon.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI);
                     } completion:^(BOOL finished) {
                         //
                     }];

}

Solution

  • hi you need code like this...

    in the .h file u declare one CGAffineTransform variable say initial....

    in viewDidLoad you need to store the initial transform value by this...

    initial=self.icon.transform;
    

    then you need to change your method to this type...

       -(void) onTap:(UITapGestureRecognizer *)tap{
    
            CGPoint point = [tap locationInView:self.view];
    
    
    
            [UIView animateWithDuration:1.2
                                  delay:0
                                options:0
                             animations:^{
                                 self.icon.center = point;
                             } completion:^(BOOL finished) {
                                 //
                             }];
    
            [UIView animateWithDuration:1.2
                                  delay:0
                                options:0
                             animations:^{
    
    if (self.icon.transform.a  == initial.a) 
                             {
                                 self.icon.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI);
                             }
                             else
                             {
                                 self.icon.transform = initial;
                             }
    
    
                             } completion:^(BOOL finished) {
                                 //
                             }];
    
        }
    

    this is an idea you need to implement your logic with it....