iOS开发常用的几种动画效果

39 篇文章 0 订阅
11 篇文章 0 订阅

1.block代码块动画

/**
 *  大图
 */
- (IBAction)bigImg {
    // 1.添加阴影
    UIButton *cover = [[UIButton alloc] init];
    cover.frame = self.view.bounds;
    cover.backgroundColor = [UIColor blackColor];
    cover.alpha = 0.0;
    [cover addTarget:self action:@selector(smallImg) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cover];
    self.cover = cover;
    
    // 2.更换阴影和头像的位置
    [self.view bringSubviewToFront:self.iconBtn];
    
    // 3.执行动画
    [UIView animateWithDuration:0.25 animations:^{
        // 3.1.阴影慢慢显示出来
        cover.alpha = 0.7;
        
        // 3.2.头像慢慢变大,慢慢移动到屏幕的中间
        CGFloat iconW = self.view.frame.size.width;
        CGFloat iconH = iconW;
        CGFloat iconY = (self.view.frame.size.height - iconH) * 0.5;
        self.iconBtn.frame = CGRectMake(0, iconY, iconW, iconH);
    }];
}

/**
 *  小图
 */
- (void)smallImg
{
    // 执行动画
    [UIView animateWithDuration:0.25 animations:^{
        // 存放需要执行动画的代码
        
        // 1.头像慢慢变为原来的位置和尺寸
        self.iconBtn.frame = CGRectMake(85, 80, 150, 150);
        
        // 2.阴影慢慢消失
        self.cover.alpha = 0.0;
    } completion:^(BOOL finished) {
        // 动画执行完毕后会自动调用这个block内部的代码
        
        // 3.动画执行完毕后,移除遮盖(从内存中移除)
        [self.cover removeFromSuperview];
        self.cover = nil;
    }];
}

评注:

(1)在实际的开发中更常用的时block代码块来处理动画操作。

(2)block代码块动画相对来说比较灵活,尤为重要的是能够将动画相关的代码编写在一起,便于代码的阅读和理解.

        以上代码片段来源:http://blog.csdn.net/sunnyboy9/article/details/15811429

2.首尾式动画

- (IBAction)moveAndBig
{
    // 1.开启动画,下面两行代码是让下面坐标和图片大小慢慢的改变
    [UIView beginAnimations:nil context:nil];
	//让这个动画持续2秒(慢慢变化)
    [UIView setAnimationDuration:2.0];
    
    // 2.修改属性
    CGRect tempF = self.head.frame;
    tempF.origin.x += 50;
    tempF.origin.y += 100;
    
    tempF.size.width += 50;
    tempF.size.height += 50;
    self.head.frame = tempF;
    
    // 3.提交动画
    [UIView commitAnimations];
}

评注:如果只是修改控件的属性,使用首尾式动画还是比较方便的,但是如果需要在动画完成后做后续处理,就不是那么方便了

3.序列帧动画

//
//  MJViewController.m

#import "MJViewController.h"

@interface MJViewController ()
- (IBAction)knockout;
- (IBAction)stomach;
- (IBAction)footLeft;
- (IBAction)footRight;
- (IBAction)angry;
- (IBAction)cymbal;
- (IBAction)drink;
- (IBAction)eat;
- (IBAction)fart;
- (IBAction)pie;
- (IBAction)scratch;

@property (weak, nonatomic) IBOutlet UIImageView *tom;
@end

@implementation MJViewController

#pragma mark执行动画
- (void)animationWithImgName:(NSString *)imgName imgCount:(int)imgCount
{
    if (self.tom.isAnimating) return;
    
    // 1.加载动画图片
    NSMutableArray *animationImages = [NSMutableArray array];
    for (int i = 0; i<imgCount; i++) {
        // 1.1.文件名
        NSString *name = [NSString stringWithFormat:@"%@_%02d", imgName, i];
        
        // 1.2.全路径
        NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"jpg"];
        
        // 1.3.加载图片
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        
        // 1.4.添加图片
        [animationImages addObject:image];
    }
    
    self.tom.animationImages = animationImages;
    
    // 2.动画时间
    self.tom.animationDuration = animationImages.count * 0.1;
    
    // 3.只执行一次
    self.tom.animationRepeatCount = 1;
    
    // 4.开始执行动画
    [self.tom startAnimating];
    
    // 5.清空图片
    [self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];
}

#pragma mark 打击头部
- (IBAction)knockout {
    [self animationWithImgName:@"knockout" imgCount:81];
}

#pragma mark 打击肚子
- (IBAction)stomach {
    [self animationWithImgName:@"stomach" imgCount:34];
}

- (IBAction)footLeft {
    [self animationWithImgName:@"footLeft" imgCount:30];
}

- (IBAction)footRight {
    [self animationWithImgName:@"footRight" imgCount:30];
}

- (IBAction)angry {
    [self animationWithImgName:@"angry" imgCount:26];
}

- (IBAction)cymbal {
    [self animationWithImgName:@"cymbal" imgCount:13];
}

- (IBAction)drink {
    [self animationWithImgName:@"drink" imgCount:81];
}

- (IBAction)eat {
    [self animationWithImgName:@"eat" imgCount:40];
}

- (IBAction)fart {
    [self animationWithImgName:@"fart" imgCount:28];
}

- (IBAction)pie {
    [self animationWithImgName:@"pie" imgCount:24];
}

- (IBAction)scratch {
    [self animationWithImgName:@"scratch" imgCount:56];
}


@end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值