iOS图片折叠


#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UIImageView *topImageView;

@property (strong, nonatomic) IBOutlet UIImageView *bottomImageView;

@property (nonatomic, weak) CAGradientLayer *gradientLayer;

@end

@implementation ViewController

- (IBAction)pan:(UIPanGestureRecognizer *)sender {


    //获取移动偏移量

     CGPointtransP = [sendertranslationInView:sender.view];

    //让上部分图片开始旋转

    CGFloatangle = transP.y*M_PI/360;

    NSLog(@"%f",transP.y*1/360.0);

    //self.gradientLayer.opacity = transP.y * 1 / 360;


    //近大远小效果

    CATransform3D transform = CATransform3DIdentity;

    //  眼睛离屏幕的距离

    transform.m34= -1/300.0;

//    self.topImageView.layer.transform = CATransform3DMakeRotation(-angle, 1, 0, 0);

    self.topImageView.layer.transform=CATransform3DRotate(transform, -angle,1,0,0);


    if (sender.state == UIGestureRecognizerStateEnded) {


        self.gradientLayer.opacity=NO;


        //上面的图片复位

        //usingSpringWithDamping:弹性系数,越小弹性越大

        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{

            self.topImageView.layer.transform = CATransform3DIdentity;

        }completion:nil];


    }

}

- (void)viewDidLoad {

    [super viewDidLoad];


    //1、让上半部分图片只显示上半部分

    self.topImageView.layer.contentsRect=CGRectMake(0,0,1,0.5);

    //2、让下半部分图片只显示下半部分

    self.bottomImageView.layer.contentsRect=CGRectMake(0,0.5,1,0.5);


    self.topImageView.layer.anchorPoint = CGPointMake(0.5, 1);

    self.bottomImageView.layer.anchorPoint = CGPointMake(0.5, 0);


    //渐变层

    CAGradientLayer *gradientLayer = [CAGradientLayer layer];

    gradientLayer.frame=self.bottomImageView.bounds;

    //设置渐变颜色

    gradientLayer.colors=@[(id)[UIColorclearColor].CGColor,(id)[UIColorblackColor].CGColor];

    //开始设置渐变为透明

    gradientLayer.opacity=0;


    self.gradientLayer= gradientLayer;


    [self.bottomImageView.layeraddSublayer:gradientLayer];


}

//渐变层

-(void)gradientLayer{

    //渐变层

    CAGradientLayer *gradientLayer = [CAGradientLayer layer];

    gradientLayer.frame=self.bottomImageView.bounds;

    //设置渐变颜色

    gradientLayer.colors = @[(id)[UIColor redColor].CGColor,(id)[UIColor greenColor].CGColor,(id)[UIColor yellowColor].CGColor];

    //设置渐变方向

    gradientLayer.startPoint=CGPointMake(0,0);

    gradientLayer.endPoint=CGPointMake(1,0);

    //设置一个渐变到另一个渐变的起始位置

    gradientLayer.locations=@[@0.2,@0.6];

    [self.bottomImageView.layeraddSublayer:gradientLayer];


}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

你可能感兴趣的:(iOS图片折叠)