iOS 动画 —— 吃豆豆

一个吃豆豆的小动画,蛮简单的。

eat peas
#import "ViewController.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

static const CGFloat kPearsWidth = 30.0f;
static const CGFloat kPearsHeight = 30.0f;
static const CGFloat kEatPicWidth = 60.0f;
static const CGFloat kEatPicHeight = 60.0f;
static const CGFloat kSpaceButtonHeight = 120.f; // 预留高度

@interface ViewController ()

@property (nonatomic, strong) UIImageView *eatImageView;
@property (nonatomic, strong) UIImageView *peasImageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 添加
    [self.view addSubview:self.peasImageView];
    [self.view addSubview:self.eatImageView];
    // 并大致设置其位置
    self.eatImageView.center = CGPointMake((SCREEN_WIDTH - kPearsWidth - kEatPicWidth)/2.0, 120);
    self.peasImageView.center = CGPointMake((SCREEN_WIDTH - kPearsWidth - kEatPicWidth)/2.0 + kPearsWidth + kEatPicWidth, 120);
    
}

- (void)showPeasImageView {
    // 获取随机位置
    self.peasImageView.center = CGPointMake((arc4random() % (int)(SCREEN_WIDTH - kEatPicWidth)) + kEatPicWidth, (arc4random() % (int)(SCREEN_HEIGHT - kEatPicHeight - kSpaceButtonHeight)) + kEatPicHeight);
    // 显示
    self.peasImageView.hidden = NO;
}

- (UIImageView *)peasImageView {
    if (!_peasImageView) {
        _peasImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kPearsWidth, kPearsHeight)];
        _peasImageView.image = [UIImage imageNamed:@"peas"];
    }
    return _peasImageView;
}

- (UIImageView *)eatImageView {
    if (!_eatImageView) {
        _eatImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kEatPicWidth, kEatPicHeight)];
        _eatImageView.image = [UIImage imageNamed:@"eatRight"];
    }
    return _eatImageView;
}

/**
  按钮是通过 StoryBoard 创建的
 */
- (IBAction)upAction:(id)sender {
    
    if (self.eatImageView.center.y > self.eatImageView.frame.size.height) {
        // 向上移动10
        [UIView animateWithDuration:0.5 animations:^{
            self.eatImageView.image = [UIImage imageNamed:@"eatUp"];
            self.eatImageView.center = CGPointMake(self.eatImageView.center.x, self.eatImageView.center.y - 10);
            // 判断其是否可以吃掉豆子啦
            if (self.eatImageView.center.x - (kEatPicWidth / 2) < self.peasImageView.center.x && self.eatImageView.center.x + (kEatPicWidth / 2)>self.peasImageView.center.x && self.eatImageView.center.y - (kPearsWidth/2) self.peasImageView.center.y) {
                self.peasImageView.hidden=YES;
                [self performSelector:@selector(showPeasImageView) withObject:self afterDelay:0.8];
            }
        
        }];
    }
}

- (IBAction)downAction:(id)sender {
    // kSpaceButtonHeight 是四个按钮预留的距离
    if (self.eatImageView.center.y < (SCREEN_HEIGHT - self.eatImageView.frame.size.height - kSpaceButtonHeight)) {
        [UIView animateWithDuration:0.5 animations:^{
            self.eatImageView.center=CGPointMake(self.eatImageView.center.x, self.eatImageView.center.y+10);
            self.eatImageView.image=[UIImage imageNamed:@"eatDown"];
            // 判断其是否可以吃掉豆子啦
            if (self.eatImageView.center.x - (kEatPicWidth / 2) < self.peasImageView.center.x && self.eatImageView.center.x + (kEatPicWidth / 2)>self.peasImageView.center.x && self.eatImageView.center.y - (kPearsWidth/2) self.peasImageView.center.y) {
                self.peasImageView.hidden=YES;
                [self performSelector:@selector(showPeasImageView) withObject:self afterDelay:0.8];
            }

        }];
    }
}

- (IBAction)leftAction:(id)sender {
    
    if (self.eatImageView.center.x > self.eatImageView.frame.size.width) {
        // 向上移动10
        [UIView animateWithDuration:0.5 animations:^{
            self.eatImageView.center=CGPointMake(self.eatImageView.center.x-10, self.eatImageView.center.y);
            self.eatImageView.image=[UIImage imageNamed:@"eatLeft"];
            // 判断其是否可以吃掉豆子啦
            if (self.eatImageView.center.x - (kEatPicWidth / 2) < self.peasImageView.center.x && self.eatImageView.center.x + (kEatPicWidth / 2)>self.peasImageView.center.x && self.eatImageView.center.y - (kPearsWidth/2) self.peasImageView.center.y) {
                self.peasImageView.hidden = YES;
                [self performSelector:@selector(showPeasImageView) withObject:self afterDelay:0.8];
            }
        }];
    }
}

- (IBAction)rightAction:(id)sender {

    if (self.eatImageView.center.x < (SCREEN_WIDTH - self.eatImageView.frame.size.width)) {
        [UIView animateWithDuration:0.5 animations:^{
            
            self.eatImageView.center=CGPointMake(self.eatImageView.center.x+10, self.eatImageView.center.y);
            self.eatImageView.image=[UIImage imageNamed:@"eatRight"];
            // 判断其是否可以吃掉豆子啦
            if (self.eatImageView.center.x - (kEatPicWidth / 2) < self.peasImageView.center.x && self.eatImageView.center.x + (kEatPicWidth / 2)>self.peasImageView.center.x && self.eatImageView.center.y - (kPearsWidth/2) self.peasImageView.center.y) {
                self.peasImageView.hidden = YES;
                [self performSelector:@selector(showPeasImageView) withObject:self afterDelay:0.8];
            }
        }];
    }
}

@end

说白了,就是位置的改变的和图像的隐藏,此处作为熟悉用。

PS:场景来自:【iOS开发范例实战宝典.进阶篇】。

你可能感兴趣的:(iOS 动画 —— 吃豆豆)