启动页面特效--轮转

2016-07-29  本文已影响21人  hunterzhu

编写了一个简单而有趣的图片轮转特效。
这其中写了一些坐标的运算。
效果图:


1
2

代码部分

//
//  ViewController.m
//  demo-启动页特效
//
//  Created by mac on 16/7/29.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"
#define kScreenWidth     [UIScreen mainScreen].bounds.size.width
#define kScreenHeight     [UIScreen mainScreen].bounds.size.height
@interface ViewController (){
    //图片视图的数组
    NSMutableArray *imgViews;
    //图片视图的索引
    int index;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化
    imgViews = [NSMutableArray array];
    index = 0;
    self.view.backgroundColor = [UIColor blackColor];
    
    //1.创建图片数组
    //设置24张图片  -每张图片的高度和宽度
    CGFloat width = kScreenWidth/4;
    CGFloat height = kScreenHeight/6;
    CGFloat x = 0;
    CGFloat y = 0;
    for(int i = 0; i < 24; i++ ){
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat: @"%d",i + 1]];
        UIImageView *imageView = [[UIImageView alloc ] initWithImage:image];
    //2.建立坐标点
        
        if (i<4) {
            x = i * width;
            y = 0;
        }else if (i<8){
            x = 3 *width;
            y = (i - 3) * height;
        }else if(i<12){
            x = kScreenWidth - (i - 7) *width;
            y = kScreenHeight - height;
        }else if (i <16) {
            x = 0;
            y = kScreenHeight - (i - 10) *height;
            
        }else if(i <18){
            x = (i - 15) *width;
            y = height;
            
        }else if (i<21){
            x = 2 * width;
            //两个高度单位
            y = (i - 16) *height;
        }else if(i <24){
            x = width;
            //5个高度的单位开始
            y = kScreenHeight - (i - 19) *height;
        }
        //设置frame
        imageView.frame = CGRectMake(x, y, width, height);
        //设置隐藏
        imageView.alpha = 0.3;
        [self.view addSubview:imageView];
        [imgViews addObject:imageView];
    
    }
    //3.添加定时器
    [NSTimer scheduledTimerWithTimeInterval:0.01
                                     target:self
                                   selector:@selector(changeAction:)
                                   userInfo:nil
                                    repeats:YES];
    
}

- (void)changeAction:(NSTimer *)timer {
    //数组超越界限
    if (index > 48) {
        index = 0;
    }
    
    if (index < 24) {
        UIImageView *imageView = imgViews[index];
        //显示出来
        imageView.alpha = 1;
    }else if (index > 24){
        UIImageView *imageView = imgViews[48- index];
        //隐藏
        imageView.alpha = 0.3;
    }
    
    index ++;
}

@end

上一篇下一篇

猜你喜欢

热点阅读