效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 - (UIImageView *)nextView; 6 - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context; 7 @end 8 9 @implementation ViewController 10 #define kViewTag 1 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 15 [self layoutUI]; 16 } 17 18 - (void)didReceiveMemoryWarning { 19 [super didReceiveMemoryWarning]; 20 // Dispose of any resources that can be recreated. 21 } 22 23 - (void)layoutUI { 24 self.view.backgroundColor = [UIColor blackColor]; 25 26 [self.view addSubview:[self nextView]]; 27 } 28 29 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 30 static UIViewAnimationTransition transition = UIViewAnimationTransitionNone; 31 UIImageView *imgVCurrent = [self nextView]; 32 33 [UIView beginAnimations:nil context:NULL]; 34 [UIView setAnimationDuration:2.0]; 35 [UIView setAnimationDelegate:self]; 36 [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 37 [UIView setAnimationTransition:transition forView:self.view cache:YES]; 38 [[self.view viewWithTag:kViewTag] removeFromSuperview]; 39 [self.view addSubview:imgVCurrent]; 40 [UIView commitAnimations]; 41 42 //将动画设置为无效状态 43 [UIView setAnimationsEnabled:NO]; 44 //切换过渡效果 45 switch (transition) { 46 case UIViewAnimationTransitionNone: //无过渡效果;作为默认值 47 case UIViewAnimationTransitionFlipFromLeft: //从左翻转效果 48 case UIViewAnimationTransitionFlipFromRight: //从右翻转效果 49 case UIViewAnimationTransitionCurlUp: { //从下往上翻页效果 50 transition++; 51 break; 52 } 53 case UIViewAnimationTransitionCurlDown: { //从上往下翻页效果 54 transition = UIViewAnimationTransitionNone; 55 break; 56 } 57 } 58 } 59 60 - (UIImageView *)nextView { 61 static BOOL isFirstView = YES; 62 UIImage *img = [UIImage imageNamed: (isFirstView ? @"Animation1" : @"Animation2")]; 63 isFirstView = !isFirstView; 64 65 UIImageView *imgV = [[UIImageView alloc] initWithImage:img]; 66 imgV.tag = kViewTag; 67 CGFloat widthOfImg = img.size.width * 3; //以原图片宽度的3倍放大,作为图片视图的图片宽度 68 CGFloat heightOfImg = img.size.height * 3; //以原图片高度的3倍放大,作为图片视图的图片高度 69 imgV.frame = CGRectMake(CGRectGetMidX(self.view.frame) - widthOfImg/2, 70 CGRectGetMidY(self.view.frame) - heightOfImg/2 + 10, 71 widthOfImg, 72 heightOfImg); 73 return imgV; 74 } 75 76 - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 77 //将动画设置为有效状态 78 [UIView setAnimationsEnabled:YES]; 79 } 80 81 @end
时间: 2024-10-15 03:35:30