iOS 事件处理 geekband

2016-04-07  本文已影响31人  AAup

事件(UIEvent)是一个对象,被发送给 Application,包含了一些信息来告诉我们的程序用户干了些什么。事件在 iOS 中主要包含3种:MultiTouch Events、Accelerometer Events、Remote Control Events。

MultiTouch Events

用户触摸屏幕产生的事件或 Smart Pencil 接触屏幕产生的事件,如点击、捏合等等

Accelerometer Events

传感器产生的事件,如用户摇晃手机等。

Remote Control Events

使用遥控器或耳机线控等产生的事件。如播放、暂停等。

UIResponder


Responder Chain


传递


在自定义类里响应触摸事件


多点触摸事件



案例

新建项目


新建文件


并在.m下
输入

 -(UIView *)findToucheSubview:(CGPoint)point withEvent:(UIEvent*)event{
for (UIView * v in self.subviews) {
    CGPoint cp = [v convertPoint:point fromView:self];
    if ([v pointInside:cp withEvent:event])
        return v;
}
return nil;
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
for (UITouch *touch in touches) {
    if (touch.phase == UITouchPhaseMoved) {
        CGPoint loc = [touch locationInView:self];
        UIView *touchedView = [self findToucheSubview:loc
                                            withEvent:event];
        if (touchedView != nil) {
            CGPoint lastloc = [touch previousLocationInView:self];
            CGVector moved = CGVectorMake(loc.x - lastloc.x , loc.y - lastloc.y);
            [touchedView setFrame:CGRectOffset(touchedView.frame, moved.dx, moved.dy)];
        }
    }       
}
}

在storyboard 加UIView


加入image


设置UIView的Class


run


上一篇 下一篇

猜你喜欢

热点阅读