码迷,mamicode.com
首页 > 其他好文 > 详细

165在屏幕中实现过渡动画效果

时间:2015-06-17 17:56:24      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

效果如下:

技术分享

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

 

165在屏幕中实现过渡动画效果

标签:

原文地址:http://www.cnblogs.com/huangjianwu/p/4583929.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!