美文网首页ui问题
iOS动画--翻页动画

iOS动画--翻页动画

作者: cloud_333 | 来源:发表于2017-02-24 15:20 被阅读565次

           最近工作中遇到一个首页要实现翻页动画的需求,上网搜索未找到自己满意的方案,索性自己写了一份,主要利用了CoreGraphics框架进行的图片处理,在这里与大家分享实现思路,若大家有更好的方法可以与我交流交流。

            首页按日期加载多日的资讯,每次上滑或者下滑显示另一天资讯时,需要用到翻页动画。下面是实现效果。

    翻页动画

    这里采用CATransform3D来实现,思路如下:

    1,向后台请求资讯数据。

    2,渲染每日的UIView。并在图片加载完成之后对其进行截图,得到以中线分割的两个UIImage对象。

         在YYKit的网络图片处理库中,图片加载完成会在block得到。可用临时数组标示当日View是否加载完成,完成后替换切割图片,替换对应的展示图片。

    图片加载完成可以在block回调中得知

    同时开启定时器,定时截图,避免网络不好,加载过慢。

    渲染完成之后进行截图,并缓存。下面是截图代码

    - (UIImage*)captureImageFromView:(UIView*)view isUp:(BOOL)up{

    CGRectscreenRect =CGRectMake(0,0,SCREEN_WIDTH,(SCREEN_HEIGHT-64)/2.0);

    if(!up) {

    screenRect=CGRectMake(0,(SCREEN_HEIGHT-64)/2.0,SCREEN_WIDTH,(SCREEN_HEIGHT-64)/2.0);

    }

    UIGraphicsBeginImageContextWithOptions(screenRect.size,NO, [UIScreenmainScreen].scale);

    CGContextRefcontext =UIGraphicsGetCurrentContext();

    if(context ==NULL){

    returnnil;

    }

    //copy一份图形上下位文,用来操作

    CGContextSaveGState(context);

    //将当前图行位文进行矩阵变换

    CGContextTranslateCTM(context, -screenRect.origin.x, -screenRect.origin.y);

    [view.layerrenderInContext:context];

    //图形位文退出栈

    CGContextRestoreGState(context);

    UIImage*image =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    returnimage;

    }

    3,缓存所有的UIImage对象,通过UIPanGestureRecognizer与CATransform3D实现翻页动画。

           首先,在上面两个展示的UIImageView对象加入Pan手势,并设置其锚点。

    self.upImageView.layer.anchorPoint=CGPointMake(0.5,1.0);

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

          在layer层的tranform属性可以是CATransform3D类型的四维仿射变换矩阵,并且提供预置好的进行旋转、变形之后的仿射变换矩阵,我们只需改变CATransform3D的transform属性就可以得到想要的动画效果。在Pan手势上滑过程中,需要一张临时图片在下面,frame跟所对应的展示ImageView一样,同时当前ImageView的layer的tranform属性根据滑动距离进行改变。

    self.upImageView.layer.transform= [self transForm3DWithAngle:angle];

    -(CATransform3D)TransForm3DWithAngle:(CGFloat)angle{

    CATransform3Dtransform =CATransform3DIdentity;

    transform.m34= -2.0/2000;

    transform=CATransform3DRotate(transform,angle,1,0,0);

    return transform;

    }

    CATransform3DRotate 是3D旋转API。

    CATransform3DCATransform3DRotate (CATransform3Dt,CGFloatangle,CGFloatx,CGFloaty,CGFloatz)

           第一个参数为transform对象,第二个为旋转角度,第三,四,五个为旋转轴。m34的只是CATransform3D结构体中的属性,m34属性可以实现透视效果,增加翻转的3D效果。

           在旋转超过90度时候,需要替换当前旋转图片,并做CTM替换。

    UIGraphicsBeginImageContext(rect.size);

    CGContextRefcontext =UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);

    //做CTM变换

    //CGContextTranslateCTM(context, 0.0, rect.size.height);

    //CGContextScaleCTM(context, 1.0, -1.0);

    //CGContextRotateCTM(context, rotate);

    //CGContextTranslateCTM(context, translateX, translateY);

    CGContextScaleCTM(context, scaleX, scaleY);

    //绘制图片

    CGContextDrawImage(context,CGRectMake(0,0, rect.size.width, rect.size.height), image.CGImage);

    CGContextRestoreGState(context);

    UIImage*newPic =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

            用户松手之后,开启定时器,根据Pan手势的滑动趋势或当前滑动的角度决定是否翻页还是还原。

    相关文章

      网友评论

      本文标题:iOS动画--翻页动画

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