码迷,mamicode.com
首页 > 其他好文 > 详细

进击的UI---------------------UIEvent&UITouch&UIResponder

时间:2015-11-21 18:20:37      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

 
1.UIEvent(事件)
1??:事件分为三种:1 触摸事件 2 晃动事件 3 远程控制
2??:触摸事件

//1. UIControlEventTouchUpInside     点击进去(点后松手)

//2. UIControlEventTouchDown         单击(点就出)

//3. UIControlEventTouchDownRepeat   双击

//4. UIControlEventTouchDragInside   点一点,拽一拽(Button内)

//5. UIControlEventTouchDragOutside  点一点,拽一拽(Button外)

//6. UIControlEventTouchDragEnter    点住(拖出去,拖进来)

//7. UIControlEventTouchDragExit     点住(拖出去)

//8. UIControlEventTouchUpOutside    点击拉出Button

//9. UIControlEventTouchCancel       触发不了

//10. UIControlEventValueChanged     UISlider

2.UITouch(触摸)
1??:UIView实现触摸方法需要重写Touch有关方法
2??:
①:- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event

    NSLog(@"开始摸了~~");

    // UITouch 表示屏幕上的一根手指

    UITouch *touch = [touches anyObject];

    // 获取触摸屏幕的坐标

    CGPoint startPoint = [touch locationInView:self];

    // 打印坐标

    NSLog(@"%@",NSStringFromCGPoint(startPoint));

}

②:- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    NSLog(@"正在摸~~");

    self.backgroundColor = [UIColor colorWithRed:arc4random()%2/1.0 green:arc4random()%2/1.0 blue:arc4random()%2/1.0 alpha:1];

    UITouch *touch = [touches anyObject];

    CGPoint movePoint  = [touch locationInView:self];

    NSLog(@"%@",NSStringFromCGPoint(movePoint));

}

③:- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{

    NSLog(@"不摸了~~");

}

④:- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    NSLog(@"摸完了~~");

 
}
3.UIResponder(响应者链)
1??:主要内容:

响应者链:多个响应者组成的链

首先执行检测过程,信息先从UIApplication传到Appdelegate->UIWindow->Controler->UIView->View的子类(确认在那个试图上产生的事件)

响应过程:先从View的子类里面寻找有没有事件的实现方法,如果有,事件执行,响应链完成,如果没有就网上一层寻找,最后找到Application,如果没有找到就会被丢弃.

响应链可以使用userInteractionEnable的属性进行检测(阻断);

4.代码:

动画Tom猫关键代码

 _image  = [[UIImageView alloc] init];

    _image.frame = CGRectMake(0, 0, 375, 670);

    _image.image = [UIImage imageNamed:@"000"];

    [self addSubview:_image];

    _angry = [UIButton buttonWithType:UIButtonTypeSystem];

    _angry.frame = CGRectMake(80, 215, 215, 110);

    [_angry addTarget:self action:@selector(angryAction) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:_angry];

 _cymbal = [UIButton buttonWithType:UIButtonTypeSystem];

    _cymbal.frame = CGRectMake(20, 400, 60, 60);

    [_cymbal addTarget:self action:@selector(cymbalAction) forControlEvents:UIControlEventTouchUpInside];

    [_cymbal setBackgroundImage:[UIImage imageNamed:@"cymbal.png"] forState:UIControlStateNormal];

    [self addSubview:_cymbal];

- (void)tomAnimation:(NSString *)img count:(int)count

{

    if ([self.image isAnimating]) return;

    NSMutableArray *arrayM = [NSMutableArray array];

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

        NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg", img, i];

        NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];

        UIImage *image = [UIImage imageWithContentsOfFile:path];

        [arrayM addObject:image];

    }

    [self.image setAnimationImages:arrayM];

    // 2. 设置动画时长

    [self.image setAnimationDuration:arrayM.count * 0.075];

    [self.image setAnimationRepeatCount:1];

    // 3. 开始动画

    [self.image startAnimating];

    // 4. 动画完成之后,再清除动画数组内容

    [self.image performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.image.animationDuration];

}

- (void)angryAction

{

    [self tomAnimation:@"angry" count:25];

}

 

 

 

进击的UI---------------------UIEvent&UITouch&UIResponder

标签:

原文地址:http://www.cnblogs.com/sharkHZ/p/4984134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!