您的位置:首页 > 产品设计 > UI/UE

iOS之UI高级---触摸事件

2015-10-20 14:10 369 查看

1.UITouch的基本使用

①事件类型:

iOS中有多种形式的事件:触摸事件、运动事件、远程控制事件等

PS:此处主要讲解触摸事件

②触摸事件:

当用户触摸屏幕时,事件会被封装成一个event实例,包含了触摸事件的相关信息。

触摸—>捕捉此事件,创建一个UIEvent对象—>将次对象加入当前应用程序的事件队列中—>由UIApplication对象 从队列中,一个个取出来分发—>首先发给UIWindow—>UIWindow分发给触摸的视图对象(第一响应者对象)

③事件的传递(响应者链)

响应者链:事件—>第一响应者对象处理(如果不处理,事件被沿着响应者链向上传递,交给下一个响应者,当然可以加一些判断来决定是否需要继续传递)



④UITouch使用代码实例:

实例一:了解触摸事件的基本使用

// 当有一个或者多个手指 开始触摸屏幕时调用
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"开始了一次触摸");

//---------------------UITouch---------------------
// 从NSSet从 取出一个触摸对象
UITouch *touch = [touches anyObject];

// 获取触摸所在的window    (同一个window)
NSLog(@"触摸所在的window %@", touch.window);
NSLog(@"当前视图所在的window%@", self.window);

// 获取触摸所在的View      (同一个View)
NSLog(@"触摸所在的View:%@, 当前视图所在的View = %@", touch.view, self);

// 触摸的位置 相对于某个视图的坐标点
CGPoint point = [touch locationInView:self.superview];
NSLog(@"point x = %f, y = %f", point.x, point.y);

}


实例二:点击某视图,可拖动视图

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// 方法一 :根据两次触摸之间的偏移 来改变视图的位置

// 当触摸移动时,视图跟着触摸一起移动
// 1. 获取触摸对象
UITouch *touch = [touches anyObject];
// 2. 获取触摸的位置
CGPoint point = [touch locationInView:self.superview];
// 获取前一个触摸点的位置
CGPoint prePoint = [touch previousLocationInView:self.superview];

// 计算位置的改变
CGPoint changed = CGPointMake(point.x - prePoint.x, point.y - prePoint.y);

// 3. 改变视图位置
self.center = CGPointMake(self.center.x + changed.x, self.center.y + changed.y);

}


实例三:用UIView实现UIButton功能(以下提供了自定义Button类的具体实现,在父类中通过子类中定义好的初始化方法,传递正确的参数即可实现和Button类似的功能)

#import "MyButton.h"

@interface MyButton()
{
id _target;
SEL _selector;

//点击开始的时间
NSTimeInterval _time;
}

@end

@implementation MyButton

- (instancetype)initWithFrame:(CGRect)frame target:(id)target selector:(SEL)selector
{
self = [super initWithFrame:frame];
if (self)
{
_target = target;
_selector = selector;
}

return self;
}

// 按钮的点击
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

// 调用SEL方法
if ([_target respondsToSelector:_selector])
{
[_target performSelector:_selector];
}

// 获取点击开始的时间
NSDate *date = [NSDate date];
_time = [date timeIntervalSince1970];

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// 获取触摸对象
UITouch *touch = [touches anyObject];
// 获取触摸点
CGPoint point = [touch locationInView:self];

// 点击结束之后 再次读取时间,比较两个时间差
NSDate *date = [NSDate date];
NSTimeInterval endedTime = [date timeIntervalSince1970];
// 结束时间 - 开始时间
if (endedTime - _time > 3)
{
NSLog(@"长按");
}
else
{
NSLog(@"普通点击");
}

// UIView中的某个方法:判断某个点是否在视图中
BOOL isInside = [self pointInside:point withEvent:nil];
if (isInside)
{
NSLog(@"在按钮内部放开");
}
else
{
NSLog(@"在按钮外部放开");
}

}

#prama mark - 子类方法调用演示
- (void)viewDidLoad {
[super viewDidLoad];
MyButton *button = [[MyButton alloc] initWithFrame:CGRectMake(30, 30, 50, 50) target:self selector:@selector(buttonAction)];
button.backgroundColor = [UIColor orangeColor];
[self.view addSubview:button];
}

- (void)buttonAction
{
NSLog(@"按钮被点击了");
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios ui