iOS 开发学习成长之路iOS开发常用iOS 开发之旅

iOS 动画 —— transform

2016-08-14  本文已影响6018人  天空中的球

了解仿射变换,我们可以先区别一下 view.transformview.layer.transform

@property(nonatomic) CGAffineTransform transform;  // view 的属性
@property CATransform3D transform; // layer 的属性

view.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:1.0f animations:^{
   view.transform = CGAffineTransformMakeScale(2.0f, 2.0f);
}];
CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
transformAnimation.beginTime = CACurrentMediaTime();
transformAnimation.duration = 1.0f;
transformAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[view.layer addAnimation:transformAnimation forKey:@"transformAnimation"];
view.layer.transform =  CATransform3DIdentity;
[UIView animateWithDuration:1.0f animations:^{
      view.layer.transform = CATransform3DMakeScale(2.0, 2.0, 1.0);
}];

说白了两者的区别就是在 二维和 三维的区别,用CGAffineTransform 和CATransform3D 的区别。

**CGAffineTransform **

 CGAffineTransformIdentity

//位移仿射  ---- 理解为平移 (CGFloat tx,CGFloat ty) 
CGAffineTransformMakeTranslation 
CGAffineTransformTranslate 
//旋转仿射 ---- 理解为旋转 (CGFloat angle)
CGAffineTransformMakeRotation
CGAffineTransformRotate 
//缩放仿射  --- 理解缩放大小 (CGFloat sx, CGFloat sy)
CGAffineTransformMakeScale 
CGAffineTransformScale

** CATransform3D **


 CATransform3DIdentity

 //位移3D仿射  ==> (CGFloat tx, CGFloat ty, CGFloat tz)
CATransform3DMakeTranslation
CATransform3DTranslation        
//旋转3D仿射 ==> (CGFloat angle, CGFloat x, CGFloat y, CGFloat z)
CATransform3DMakeRotation
CATransform3DRotation  
//缩放3D仿射 ==>  (CGFloat angle, CGFloat x, CGFloat y, CGFloat z)
CATransform3DMakeScale
CATransform3DScale
//叠加3D仿射效果
CATransform3DConcat    
//仿射基础3D方法,可以直接做效果叠加
CGAffineTransformMake (sx,shx,shy,sy,tx,ty)
//检查是否有做过仿射3D效果  == ((CATransform3D t))
CATransform3DIsIdentity(transform)
//检查2个3D仿射效果是否相同
CATransform3DEqualToTransform(transform1,transform2)
//3D仿射效果反转(反效果,比如原来扩大,就变成缩小)
CATransform3DInvert(transform)

CATransform3D与CGAffineTransform的转换

//将一个CGAffinrTransform转化为CATransform3D
CATransform3D CATransform3DMakeAffineTransform (CGAffineTransform m);
//判断一个CATransform3D是否可以转换为CAAffineTransformbool 
CATransform3DIsAffine (CATransform3D t);
//将CATransform3D转换为CGAffineTransform
CGAffineTransform CATransform3DGetAffineTransform (CATransform3D t);

当我们改变过一个view.transform属性或者view.layer.transform的时候需要恢复默认状态的话,记得先把他 们重置为

view.transform = CGAffineTransformIdentity;
view.layer.transform = CATransform3DIdentity;

二维:

一组二维,平移,缩放,旋转

二维这块,不过多记录,看看效果就好啦,强大的是CATransform3DIdentity,好多意想不到的效果都可以构成,此处抛砖引玉,以一个经典的立体翻转效果展示它的强大,很多人都模仿过啦,源自iOS 3D UI --- CALayer的transform扩展 .

效果:


scrollerCube.gif

这是一个在 UIScollerView 并带UIPageControl的滑动展示

核心代码:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSInteger currentPage = self.pageControl.currentPage;
    //当前视图View
    UIView *currentView = self.viewArray[currentPage];
    //上一个View
    UIView *lastView = nil;
    if (currentPage-1 >= 0) {
        lastView = self.viewArray[currentPage-1];
    }
    //下一个View
    UIView *nextView = nil;
    if (currentPage+1 <= self.viewArray.count - 1) {
        nextView = _viewArray[currentPage+1];
    }
    
    //本次偏移距离
    CGFloat currentOffset = scrollView.contentOffset.x - currentPage*self.view.bounds.size.width;
    //本次偏移角度
    CGFloat angle = currentOffset/self.view.bounds.size.width * M_PI_2;
//    NSLog(@"currentOffset === %f angle === %f",currentOffset, angle);
    //当前 视图变换
    [self currentViewAnimation:currentView angle:angle offset:currentOffset];
    //下一个视图移动变幻
    [self lastViewAnimation:lastView angle:angle offset:currentOffset];
    //上一个视图移动变幻
    [self nextViewAnimaiton:nextView angle:angle offset:currentOffset];
}

CATransform3D CATransform3DMakePerspective(CGPoint center, float disZ)
{
    CATransform3D transformToCenter = CATransform3DMakeTranslation(-center.x, -center.y, 0);
    CATransform3D transformBack = CATransform3DMakeTranslation(center.x, center.y, 0);
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = -1.0f/disZ;
    return CATransform3DConcat(CATransform3DConcat(transformToCenter, transform), transformBack);
}

CATransform3D CATransform3DPerspect(CATransform3D t, CGPoint center, float disZ)
{
    return CATransform3DConcat(t, CATransform3DMakePerspective(center, disZ));
}

- (void)currentViewAnimation:(UIView *)currentView angle:(CGFloat)angle offset:(CGFloat)currentOffset {

    //设置锚点
    currentView.layer.anchorPoint = CGPointMake(0.5, 0.5);
    //向屏幕前方移动
    CATransform3D move = CATransform3DMakeTranslation(0, 0, self.view.bounds.size.width/2);
    //旋转
    CATransform3D rotate = CATransform3DMakeRotation(-angle, 0, 1, 0);
    //平移
    CATransform3D plaintMove = CATransform3DMakeTranslation(currentOffset, 0, 0);
    //向屏幕后方移动
    CATransform3D back = CATransform3DMakeTranslation(0, 0, -self.view.bounds.size.width/2);
    //连接
    CATransform3D concat = CATransform3DConcat(CATransform3DConcat(move, CATransform3DConcat(rotate, plaintMove)),back);
    CATransform3D transform = CATransform3DPerspect(concat, CGPointMake(currentOffset/2, self.view.bounds.size.height), 6000.0f);
    currentView.layer.transform = transform;
}

- (void)nextViewAnimaiton:(UIView *)nextView angle:(CGFloat)angle offset:(CGFloat)currentOffset {
    
    //设置锚点
    nextView.layer.anchorPoint=CGPointMake(0.5, 0.5);
    //向屏幕前方移动
    CATransform3D move = CATransform3DMakeTranslation(0, 0, self.view.bounds.size.width/2);
    //旋转
    CATransform3D rotate = CATransform3DMakeRotation(-angle+M_PI_2, 0, 1, 0);
    //平移
    CATransform3D plaintMove =CATransform3DMakeTranslation(currentOffset - self.view.bounds.size.width, 0, 0);
    //向屏幕后方移动
    CATransform3D back = CATransform3DMakeTranslation(0, 0, -self.view.bounds.size.width/2);
    //拼接
    CATransform3D concat = CATransform3DConcat(CATransform3DConcat(move, CATransform3DConcat(rotate, plaintMove)),back);
    CATransform3D transform = CATransform3DPerspect(concat, CGPointMake(self.view.bounds.size.width/2+currentOffset/2, self.view.bounds.size.height), 6000.0f);
    //添加变幻特效
    nextView.layer.transform = transform;

}

- (void)lastViewAnimation:(UIView *)lastView angle:(CGFloat)angle offset:(CGFloat)currentOffset {

    //设置锚点
    lastView.layer.anchorPoint=CGPointMake(0.5, 0.5);
    //向屏幕前方移动
    CATransform3D move = CATransform3DMakeTranslation(0, 0, self.view.bounds.size.width/2);
    //旋转
    CATransform3D rotate = CATransform3DMakeRotation(-angle-M_PI_2, 0, 1, 0);
    //平移
    CATransform3D plaintMove=CATransform3DMakeTranslation( currentOffset+self.view.bounds.size.width, 0, 0);
    //向屏幕后方移动
    CATransform3D back = CATransform3DMakeTranslation(0, 0, -self.view.bounds.size.width/2);
    CATransform3D concat = CATransform3DConcat(CATransform3DConcat(move, CATransform3DConcat(rotate, plaintMove)),back);
    CATransform3D transform = CATransform3DPerspect(concat, CGPointMake(-self.view.bounds.size.width/2+currentOffset/2, self.view.bounds.size.height), 6000.0f);
    //添加变幻特效
    lastView.layer.transform = transform;

}

注意点1:CATransform3DConcat 叠加3D仿射效果

很多时候,一个效果我们无法达到我们的yu'qi,必须需要组合起来,而此时CATransform3DConcat的作用就凸现来啦。

**注意点2 **: 还是那个重置问题

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger currentPage = scrollView.contentOffset.x / self.view.bounds.size.width;
    self.pageControl.currentPage = currentPage;
    //3D变幻恢复原状态
    for (UIView * view in self.viewArray) {
        view.layer.transform=CATransform3DIdentity;
    }
}

注意点3:滑动的边界显示还有问题

此处是scrollview滑动时偏移量转角度的问题,需要用反三角函数重新计算下角度就好了,这时就显现出我的数学基础原来这样啊

注意点4:那个 m34 的使用,是透视效果,要操作的这个对象要有旋转的角度,否则没有效果。

struct CATransform3D
{
  CGFloat m11, m12, m13, m14;
  CGFloat m21, m22, m23, m24;
  CGFloat m31, m32, m33, m34;
  CGFloat m41, m42, m43, m44;
};

从网上查阅得,通过矩阵控制坐标变换。( 备注来源

struct CATransform3D
{
  CGFloat m11(x缩放), m12(y切变), m13(旋转), m14();
  CGFloat m21(x切变), m22(y缩放), m23(和m32一起决定x轴的旋转), m24();
  CGFloat m31(和m13一起决定y轴的旋转),m32(和m23一起决定x轴的旋转), m33(z缩放), m34(透视效果,要操作的这个对象要有旋转的角度,否则没有效果。正直/负值都有意义);
  CGFloat m41(x平移), m42(y平移), m43(z平移), m44(初始为1);
}; 
// 参考:http://blog.csdn.net/gyjjone/article/details/50317439

其中,m34:透视效果m34= -1/M,M越小,透视效果越明显,必须在有旋转效果的前提下,才会看到透视效果。

备注参考:
http://blog.csdn.net/gyjjone/article/details/50317439
http://liuyanwei.jumppo.com/2015/11/24/iOS-affine-transfermation-animation.html?
http://www.cocoachina.com/bbs/read.php?tid=117061&page=1

上一篇下一篇

猜你喜欢

热点阅读