iOS中的一些动画。

一、CGAffineTransform类有三个动画,很实用,且用起来很简单。

1.CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty)
平移动画:设置平移量
2.CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)
缩放:设置缩放比例。仅通过设置缩放比例就可实现视图扑面而来和缩进频幕的效果。
使用最频繁,常用于点击按钮,按钮放大的效果。
3.CGAffineTransformMakeRotation(CGFloat angle)
旋转:设置旋转角度

利用上面的几个方法,可以写出下面的某个alert的显示隐藏的动画,为了显示明显,我把时间都调整为0.5,适当调整动画时间就可以得到满意的效果。

animation.gif
具体实现如下,补充下有关block用法
block的声明   
 ^(return type)(argument list) {//code block}
block的定义  
 typedef ( return type ) (^ blockNmae) (argument list)

分别定义两个block,显示隐藏动画时候用到。
//block的定义
//typedef ( return type ) (^ blockNmae) (argument list)
typedef void (^CustomAnimationBlock)(void);
typedef void (^CustomCompletionAnimationBlock)(BOOL finished);
显示动画的方法
- (void)show {
    self.bgview.transform = CGAffineTransformMakeScale(FLT_EPSILON, FLT_EPSILON);
  //block的声明 ^(return type)(argument list){//code block}
    CustomAnimationBlock expandBlock = ^{self.bgview.transform = CGAffineTransformMakeScale(1.3f, 1.3f);};
    CustomAnimationBlock identityBlock = ^{self.bgview.transform = CGAffineTransformIdentity;};
    CustomCompletionAnimationBlock completionBlock = ^(BOOL done){[UIView animateWithDuration:0.5f animations:identityBlock];};
    [UIView animateWithDuration:0.5f animations:expandBlock completion:completionBlock];
}
隐藏动画的方法
- (void)dismiss {
    CustomAnimationBlock expandBlock = ^{self.bgview.transform = CGAffineTransformMakeScale(1.1f, 1.1f);};
    CustomAnimationBlock shrinkBlock = ^{self.bgview.transform = CGAffineTransformMakeScale(FLT_EPSILON, FLT_EPSILON);};
    CustomCompletionAnimationBlock completionBlock = ^(BOOL done){[UIView animateWithDuration:0.5f animations:shrinkBlock];};
    [UIView animateWithDuration:0.5f animations:expandBlock completion:completionBlock];
}

代码下载地址

二、补充

  • edgesForExtendedLayout在iOS 7中,苹果引入了一个新的属性,叫做[UIViewController setEdgesForExtendedLayout:],
    它的默认值为UIRectEdgeAll。当你的容器是navigation controller时,默认的布局将从navigation bar的顶部开始。
    这就是为什么所有的UI元素都往上漂移了44pt。
    可以下载上面的demo尝试在AppDelgate里面修改edgesForExtendedLayout的值,可以看到明显的效果。
 UIRectEdgeNone = 0, 
UIRectEdgeTop = 1 << 0, 
UIRectEdgeLeft = 1 << 1, 
UIRectEdgeBottom = 1 << 2, 
UIRectEdgeRight = 1 << 3,
 UIRectEdgeAll = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight
  • 上下抖动效果的动画
- (void)wiggle{
 [UIView animateWithDuration:0.25f animations:^(){
 pullImageView.center = CGPointMake(pullImageView.center.x,
 pullImageView.center.y + 10.0f); } completion:^(BOOL finished){ 
[UIView animateWithDuration:0.25f animations:^(){ 
pullImageView.center = CGPointMake(pullImageView.center.x,
 pullImageView.center.y - 10.0f); } completion:^(BOOL finished) { [self startMotionEffects]; }]; }]; 
 //延迟四秒后再执行。
 [self performSelector:@selector(wiggle) withObject:nil 
afterDelay:4.0f];}
  • 加速度移动(手机移动后出现的轻微效果)的效果
- (void)startMotionEffects {
 UIInterpolatingMotionEffect * motionEffectX = [[UIInterpolatingMotionEffect alloc] 
initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis]; 
UIInterpolatingMotionEffect * motionEffectY =[[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" 
type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis]; 
motionEffectX.minimumRelativeValue = @-15.0; 
motionEffectX.maximumRelativeValue = @15.0; 
motionEffectY.minimumRelativeValue = @-15.0; 
motionEffectY.maximumRelativeValue = @15.0; 
motionEffectsGroup = [[UIMotionEffectGroup alloc] init]; 
motionEffectsGroup.motionEffects = @[motionEffectX, 
motionEffectY]; 
[pullImageView addMotionEffect:motionEffectsGroup];}
//移除加速度效果/
/[pullImageView removeMotionEffect:motionEffectsGroup];// motionEffectsGroup = nil;
  • CGRectInset
    一般用于
    1.在Touch事件中扩大按钮点击响应范围
    CGRectInset(view1.frame, -20, -20);
    2.用于下面的。自定义UITextField中控制 placeHolder 的位置,以及控制文本的位置。
// placeholder position
- (CGRect)textRectForBounds:(CGRect)bounds { return CGRectInset(bounds, 10, 10);}

// text position
- (CGRect)editingRectForBounds:(CGRect)bounds { return CGRectInset(bounds, 10, 10);}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,551评论 6 30
  • /* UIViewController is a generic controller base class th...
    DanDanC阅读 1,844评论 0 2
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,196评论 4 61
  • 也就跑出去到处逛逛,今天好像比昨天更冷了一点 晚上难得地 跟她一起做做一天中最后的家务 难得地拥有着只属于我们的时...
    Hasegawa阅读 180评论 0 0
  • 当我的亲人遭受疾病折磨时 我总是默默祈祷 将病痛转嫁到我的身上吧 以此减轻他们的痛苦 当我开始饱受疾病折磨 我不敢...
    蜗牛sister阅读 208评论 8 7