美文网首页
翻转UIView

翻转UIView

作者: CoderCurtis | 来源:发表于2017-05-08 17:27 被阅读55次
//
//  ViewController.m
//  Test
//
//  Created by mac on 17/5/8.
//  Copyright © 2017年 cai. All rights reserved.
//

#import "ViewController.h"

#define Screen_Width [UIScreen mainScreen].bounds.size.width

#define Screen_Height [UIScreen mainScreen].bounds.size.height

@interface ViewController ()
{
    NSUInteger index;
}

@property (nonatomic, strong) UIView *baseView;

@property (nonatomic, strong) UIImageView *imgView1;

@property (nonatomic, strong) UIImageView *imgView2;

@end

@implementation ViewController

#pragma mark -懒加载
- (UIView *)baseView
{
    if (!_baseView) {
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(15, 100, Screen_Width - 30, Screen_Height - 150)];
        _baseView.backgroundColor = [UIColor cyanColor];
    }
    return _baseView;
}

- (UIImageView *)imgView1
{
    if (!_imgView1) {
        _imgView1 = [[UIImageView alloc] initWithFrame:_baseView.bounds];
        _imgView1.backgroundColor = [UIColor purpleColor];
        _imgView1.image = [UIImage imageNamed:@"car1.jpg"];
        _imgView1.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _imgView1;
}

- (UIImageView *)imgView2
{
    if (!_imgView2) {
        _imgView2 = [[UIImageView alloc] initWithFrame:_baseView.bounds];
        _imgView2.backgroundColor = [UIColor purpleColor];
        _imgView2.image = [UIImage imageNamed:@"car2.jpg"];
        _imgView2.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _imgView2;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    index = 0;
    
    [self createUI];
}

#pragma mark -createUI
- (void)createUI
{
    [self.view addSubview:self.baseView];
    
    [self.baseView addSubview:self.imgView1];
    [self.baseView addSubview:self.imgView2];
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    index ++;
    
    if (index % 2 == 0) {
        //设置动画的效果
        [UIView transitionWithView:self.baseView
                          duration:2
                           options:UIViewAnimationOptionTransitionFlipFromLeft
                        animations:^{
                            //交换两个视图的位置
                            [self.baseView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
                        }
                        completion:^(BOOL finished) {
                            
                        }];
    }else {
        //设置动画的效果
        [UIView transitionWithView:self.baseView
                          duration:2
                           options:UIViewAnimationOptionTransitionFlipFromRight
                        animations:^{
                            //交换两个视图的位置
                            [self.baseView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
                        }
                        completion:^(BOOL finished) {
                            
                        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

效果:


翻转.gif

相关文章

网友评论

      本文标题:翻转UIView

      本文链接:https://www.haomeiwen.com/subject/haxvtxtx.html