UImageView翻转效果

来源:互联网 发布:芜湖编程招聘 编辑:程序博客网 时间:2024/06/04 17:43


感觉这个效果简单,但是还不错,主要还是用了在动画执行到一半的时候马上切换图片.就会达到这样的效果了.

当然之前必须设置UIImageView可以点击userInteractionEnabled为YES

关键代码如下:

- (void)viewDidLoad

{

    [superviewDidLoad];

    self.imageView.userInteractionEnabled =YES;

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(clickImage)];

    [self.imageViewaddGestureRecognizer:tapGesture];

    

}

- (void)clickImage{

    

    // 标记翻转状态

    self.isChanged = !self.isChanged;

    

    // 动画配置

    NSTimeInterval duration = 0.5;

    UIViewAnimationTransition transition =self.isChanged ?UIViewAnimationTransitionFlipFromRight :UIViewAnimationTransitionFlipFromLeft;

    

    // 提交动画

    [UIViewbeginAnimations:nilcontext:nil];

    [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

    [UIViewsetAnimationDuration:duration];

    [UIViewsetAnimationTransition:transitionforView:self.imageViewcache:NO];

    [UIViewcommitAnimations];

    

    //动画进行到一半,设置图片.关键点

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration/2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^

                   {

                       self.imageView.image = [UIImageimageNamed: self.isChanged ?@"1" : @"2" ];

                   });


}



0 0