iOS移动View点击事件(二)

来源:互联网 发布:秦美人翅膀进阶数据 编辑:程序博客网 时间:2024/05/21 09:16

今天把那个移动View点击事件整理了下,就直接粘贴代码了,在代码中我写了注释,说明代码的作用。那我就直接粘贴代码了

ViewController.h文件
////  ViewController.h//  ZQMoveViewsClick////  Created by 赵前 on 16/6/3.//  Copyright © 2016年 赵前. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end
ViewController.m文件
////  ViewController.m//  ZQMoveViewsClick////  Created by 赵前 on 16/6/3.//  Copyright © 2016年 赵前. All rights reserved.//#import "ViewController.h"#import "MoveViewsClickViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];    button.frame = CGRectMake(100, 200, 200, 40);    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];    button.backgroundColor = [UIColor blueColor];    [button setTitle:@"GoToNextPage" forState:UIControlStateNormal];    [button addTarget:self action:@selector(respondsToBtn:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];    // Do any additional setup after loading the view, typically from a nib.}- (void)respondsToBtn:(UIButton *)sender{    MoveViewsClickViewController *moveVc = [[MoveViewsClickViewController alloc]init];    [self presentViewController:moveVc animated:YES completion:^{    }];}@end
MoveViewsClickViewController.h文件
////  MoveViewsClickViewController.h//  ZQMoveViewsClick////  Created by 赵前 on 16/6/3.//  Copyright © 2016年 赵前. All rights reserved.//#import <UIKit/UIKit.h>@interface MoveViewsClickViewController : UIViewController@end
MoveViewsClickViewController.m文件
////  MoveViewsClickViewController.m//  ZQMoveViewsClick////  Created by 赵前 on 16/6/3.//  Copyright © 2016年 赵前. All rights reserved.//#import "MoveViewsClickViewController.h"#define WIDTH [UIScreen mainScreen].bounds.size.width#define HEIGHT [UIScreen mainScreen].bounds.size.height#define Tag 200@interface MoveViewsClickViewController (){    NSInteger _moveViewsCount;//要创建的移动视图的数量    NSTimer *_timer;//timer控件    NSInteger _moveTime;//设置动画执行的时间}@end@implementation MoveViewsClickViewController- (void)viewDidLoad {    [super viewDidLoad];    [self initializeUserInterface];}- (void)initializeUserInterface{    self.view.backgroundColor = [UIColor grayColor];//设置背景颜色    [self createMoveViewsCount:20];//可以根据需要设置view的数量    [self AddGesture];    [self AddTimer:10];//可以根据需要设置动画执行的时间}//视图即将出现的时候打开定时器- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    [_timer setFireDate:[NSDate distantPast]];}//视图即将消失的时候关闭定时器- (void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];    [_timer setFireDate:[NSDate distantFuture]];}#pragma mark *** Events ***//创建的手势,判断当手势hit到某个view的时候做出反应- (void)respondsToGesture:(UITapGestureRecognizer *)sender{    CGPoint touchPoint = [sender locationInView:self.view];    for (int i = (int)_moveViewsCount; i >=0; i--) {        UIView *temView = [self.view viewWithTag:(Tag + i)];        if ([temView.layer.presentationLayer hitTest:touchPoint]) {            //在此添加点击界面上某个view之后的代码            //在此点击的具体是哪个View是根据Tag值来判断的            temView.backgroundColor = [UIColor blueColor];            return;        }    }}//timer的事件,目的是在一次动画执行完成之后重新随机生成位置继续执行下一个线性动画- (void)respondsToTimer:(NSTimer *)timer{    for (int i = 0; i < (int)_moveViewsCount; i++) {        CAKeyframeAnimation *moveLayerAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];        CGPoint poi = [self returnRandomNumbers];        UIView *temView = [self.view viewWithTag:(Tag + i)];        moveLayerAnimation.values = @[[NSValue valueWithCGPoint:temView.center],[NSValue valueWithCGPoint:poi]];        temView.center = poi;        moveLayerAnimation.duration = _moveTime;        moveLayerAnimation.autoreverses = YES;        moveLayerAnimation.repeatCount = INFINITY;        moveLayerAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];        [temView.layer addAnimation:moveLayerAnimation forKey:@"move"];    }}#pragma mark *** Private Methods ***//生成中心点的随机数- (CGPoint)returnRandomNumbers{    int wid = WIDTH - 100;    int hei = HEIGHT - 124;    CGFloat w = (arc4random() % wid + 0);    CGFloat h = (arc4random() % hei + 0);    return CGPointMake(w, h);}//给self添加手势- (void)AddGesture{    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(respondsToGesture:)];    [self.view addGestureRecognizer:tap];}//初始化定时器- (void)AddTimer:(NSInteger)seconds{    _moveTime = seconds;    _timer = [NSTimer scheduledTimerWithTimeInterval:seconds target:self selector:@selector(respondsToTimer:) userInfo:nil repeats:YES];}//初始化屏幕上的view- (void)createMoveViewsCount:(NSInteger)viewsCount{    _moveViewsCount = viewsCount;    for (int i = 0; i < viewsCount; i++) {                //设置要显示的View        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];        view.backgroundColor = [UIColor colorWithRed:(double)(arc4random()%255 + 0)/255 green:(double)(arc4random()%255 + 0)/255 blue:(double)(arc4random()%255 + 0)/255 alpha:1.0];        view.userInteractionEnabled = YES;        view.center = [self returnRandomNumbers];        view.tag = Tag + i;        view.layer.anchorPoint = CGPointMake(0, 0);        [self.view.layer addSublayer:view.layer];        [self.view addSubview:view];    }}@end




0 0
原创粉丝点击