【源码】模拟炮弹动画效果,平移动画

本程序是模拟子弹无限的弹出平移的动画效果

知识点:(难点)

0,masony自适应布局(下载地址:https://github.com/Masonry/Masonry)

1,循环创建等间距的View

2,向可变数组中添加和取出这些View

3,定时器

4,循环执行

5,循环执行平移

6,加速和减速

感谢金JML小姐的无私帮助,接下来直接上代码:

//

//  ViewController.m

//  模拟炮弹的实现

//

//  Created by WBapple on 16/8/23.

//  Copyright © 2016年 王彬iOS开发者. All rights reserved.

//

#import "ViewController.h"

//适配

#import "Masonry.h"

#define MAS_SHORTHAND

#define MAS_SHORTHAND_GLOBALS

//按照iPhone6屏幕尺寸适配

// 转换宽度像素,将1080P转换成750P

#define changePixelW(P) (P / 1080.0) * 750.0

// 转换高度像素,将1080P转换成750P

#define changePixelH(P) (P / 1920.0) * 1334.0

// 将像素转换为点-宽度

#define pixelToPonitW(P) changePixelW(P) / 2.0

// 将像素转换为点-高度

#define pixelToPonitH(P) changePixelH(P) / 2.0

// 屏幕大小

#define kScreen [[UIScreen mainScreen] bounds]

// 屏幕宽度

#define kScreenW [[UIScreen mainScreen] bounds].size.width

// 屏幕高度

#define kScreenH [[UIScreen mainScreen] bounds].size.height

// 适配屏幕高度

#define FITHEIGHT(H)

    (pixelToPonitH(H)) / 667.0 * ([UIScreen mainScreen].bounds.size.height)

// 适配屏幕宽度

#define FITWIDTH(W)

    (pixelToPonitW(W)) / 375.0 * ([UIScreen mainScreen].bounds.size.width)

// 根据设备型号设置字体大小

#define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)

#define iPhone6P ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)

//字体大小适配

#define pixelToFontsize(P) (iPhone6P ? (P / 2.0 / 96.0 * 72.0) : (iPhone6 ? (P / 1.2 / 2.0 / 96.0 * 72.0) : (P / 1.3 / 2.0 / 96.0 * 72.0)))

// 将像素转换为点-圆角

#define svCornerRadius(P) changePixelW(P) / 2.0

// 状态栏的高度

#define statusBarH [[UIApplication sharedApplication] statusBarFrame].size.height

//电池栏高度

#define StatusBarH [UIApplication sharedApplication].statusBarFrame.size.height

//导航栏高度

//#define NavBarH       [UINavigationBar appearance].frame.size.height

#define NavBarH self.navigationController.navigationBar.height

@interface ViewController ()

@end

@implementation ViewController {

    NSMutableArray* array;

    UIView* view;

    BOOL jay;

    float speed;

}

- (void)viewDidLoad

{

    //

    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 200, 200)];

    label.text = @"点击屏幕出现子弹";

    [self.view addSubview:label];

    //

    UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(30, 550, 150, 40)];

    [button setTitle:@"加速" forState:UIControlStateNormal];

    button.backgroundColor = [UIColor redColor];

    button.titleLabel.font = [UIFont systemFontOfSize:pixelToFontsize(45)];

    button.layer.cornerRadius = svCornerRadius(12);

    [button addTarget:self

                  action:@selector(jiasu)

        forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    //

    UIButton* button2 = [[UIButton alloc] initWithFrame:CGRectMake(200, 550, 150, 40)];

    [button2 setTitle:@"减速" forState:UIControlStateNormal];

    button2.backgroundColor = [UIColor blueColor];

    button2.titleLabel.font = [UIFont systemFontOfSize:pixelToFontsize(45)];

    button2.layer.cornerRadius = svCornerRadius(12);

    [button2 addTarget:self

                  action:@selector(jiansu)

        forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button2];

    //创建可变数组

    array = [[NSMutableArray alloc] init];

    [super viewDidLoad];

    //创建炮弹

    for (int i = 0; i < 1000; i++) {

        view = [[UIView alloc] init];

        view.backgroundColor = [UIColor redColor];

        [self.view addSubview:view];

        [view mas_makeConstraints:^(MASConstraintMaker* make) {

            make.height.mas_equalTo(FITHEIGHT(30));

            make.width.mas_equalTo(FITHEIGHT(30));

            make.left.mas_equalTo(self.view.mas_left).with.offset(-FITHEIGHT(60000) + FITHEIGHT(60) * i);

            make.centerY.equalTo(self.view);

        }];

        //把创建的View逐一添加到可变数组中

        [array addObject:view];

    }

    //创建定时器

    NSTimer* timer = [NSTimer timerWithTimeInterval:0.1

                                             target:self

                                           selector:@selector(changeViewframe)

                                           userInfo:nil

                                            repeats:YES];

    // runloop循环执行

    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

    //speed默认值

    speed = 10;

}

- (void)changeViewframe

{

    CGAffineTransform ttransform;

    for (int i = 0; i < array.count; i++) {

        UIImageView* image = array[i];

        if (!jay) {

            ttransform = CGAffineTransformTranslate(image.transform, FITWIDTH(speed), 0);

            image.transform = ttransform;

            jay = YES;

        }

        else {

            ttransform = CGAffineTransformTranslate(image.transform, FITWIDTH(speed), 0);

            image.transform = ttransform;

            jay = NO;

        }

    }

}

- (void)jiasu

{

    speed += 10;

}

- (void)jiansu

{

    speed -= 10;

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

原文地址:https://www.cnblogs.com/wangbinios/p/5801344.html