CATransform3D动画教程

理论知识网上太多,现在直接甩上一个效果~请各路大神指点~

拖动效果如图:

#define APPWITH [UIScreen mainScreen].bounds.size.width
#define APPHEIGHT [UIScreen mainScreen].bounds.size.height

#import "ViewController.h"

@interface ViewController ()
{
    UIImageView  *_imageBg;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _imageBg = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 150, 250)];
    _imageBg.image = [UIImage imageNamed:@"11.jpg"];
    _imageBg.layer.cornerRadius = 5.0f;
    _imageBg.clipsToBounds = YES;
    [self.view addSubview:_imageBg];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(imageActionWithPan:)];
    [self.view addGestureRecognizer:pan];
    _imageBg.userInteractionEnabled = YES;
}


- (void)imageActionWithPan:(UIPanGestureRecognizer *)pan{
    CGPoint point = [pan locationInView:self.view];
    if (pan.state == UIGestureRecognizerStateChanged) {
        CGFloat xx = MIN(0.1, MAX(-0.1, (point.x - (APPWITH/2)) / (APPWITH/2)));
        CGFloat yy = MIN(0.1, MAX(-0.1, (point.y - (APPHEIGHT/2)) / (APPHEIGHT/2)));
        _imageBg.layer.transform = [self transform3DReturnM34:(- 1 / 500.0) xx:xx yy:yy];

    }
    //恢复
    if (pan.state == UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:0.5 animations:^{
            _imageBg.layer.transform = CATransform3DIdentity;
        }];
    }
}


- (CATransform3D)transform3DReturnM34:(CGFloat)m34  xx:(CGFloat)xx yy:(CGFloat)yy{
    CATransform3D trans = CATransform3DIdentity;
    trans.m34 = m34;
    trans = CATransform3DRotate(trans, M_PI  * xx, 0, 1, 0);
    trans = CATransform3DRotate(trans, M_PI  * yy, -1, 0, 0);
    return trans;
}
@end

 

posted @ 2015-10-11 20:59  梦影随风  阅读(449)  评论(0编辑  收藏  举报