UIView的转场动画- 单视图和双视图

单视图转场动画
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [UIView transitionWithView:self.imageview duration:1.0f options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
    // 这里只要设置视图翻转之后的内容就行。 self.imageview.tag
= (self.imageview.tag + 1)%self.imagelist.count; [self.imageview setImage:self.imagelist[self.imageview.tag]]; } completion:^(BOOL finished) { NSLog(@"动画完成"); }]; }

 

多视图转场动画,多视图转场动画里,被转出的视图的superview被设置为空,这时候视图会被释放。所以应该在定义成员变量的时候设置位强引用,用strong.否者因为没有对象使用子视图,子视图会被释放。


//
//  MainViewController.m
//  双视图翻转动画
//
//  Created by mac on 13-10-1.
//  Copyright (c) 2013年 mac. All rights reserved.
//

#import "MainViewController.h"

@interface MainViewController ()

@property (nonatomic,strong) UIView *subview1;
@property (nonatomic,strong) UIView *subview2;

@end

@implementation MainViewController



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIView *view1 = [[UIView alloc] initWithFrame:self.view.bounds];
    [view1 setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:view1];
    self.subview1 = view1;
    UIView *view2 = [[UIView alloc] initWithFrame:self.view.bounds];
    [view2 setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:view2];
    self.subview2 = view2;

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIView *fromview = nil;
    UIView *toview = nil;
    if (self.subview1.superview == nil) {
        fromview = self.subview2;
        toview = self.subview1;
    } else {
        fromview = self.subview1;
        toview = self.subview2;
    }
    
    
    
    [UIView transitionFromView:fromview toView:toview duration:2.0f options:UIViewAnimationOptionTransitionFlipFromTop completion:^(BOOL finished) {
        ;
        // 多视图转场的时候,转出视图的父视图会被释放
        NSLog(@"subview1 redview's superview is %@",self.subview1.superview);
        NSLog(@"subview2 blueview's superview is %@",self.subview2.superview);
        
        
    }];
    
    
    
    
}



@end

 

posted on 2013-10-01 11:28  老猫zl  阅读(369)  评论(0编辑  收藏  举报