IOS UI初级-触摸与手势的基本概念和用法

本文详细介绍了在iOS上处理触摸事件的各种方法,包括触摸、滑动、双击及运动事件的处理,并展示了如何使用手势识别器实现复杂的交互效果。
1.在iOS上,事件有多种形式
①触摸事件
②运动事件
③远程控制事件
2.触摸事件的处理方法
//开启多点触控
        self.multipleTouchEnabled = YES;

//手指点击时,触发的事件
- (
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   
//屏幕上点击手指的个数
   
NSInteger count = touches.count;
//    NSLog(@"touchesBegan %ld",count);
   
//拿到一个点击的对象
   
UITouch *touch = touches.anyObject;
   
UIView *touchView = touch.view;
   
NSLog(@"self---%@  touchView-----%@", self, touchView);//同一个
   
UIWindow *touchWindow = touch.window;
   
NSLog(@"self window-----%@, touchWindow---%@",self.window, touchWindow);//同一个
   
   
//拿到点击次数
   
NSInteger tapCount = touch.tapCount;
//    NSLog(@"%ld", tapCount);
   
if (tapCount == 1) {
//        NSLog(@"单击");
       
//延迟调用
        [
self performSelector:@selector(singleTap) withObject:nil afterDelay:0.5];
//        [self singleTap];
    }
else if (tapCount == 2){
//        NSLog(@"双击");
       
//取消某个方法
        [
NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
        [
self doubleTap];
    }
}

//手指滑动时,触发的事件
- (
void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//    NSLog(@"touchesMoved");
   
//取到一个touch对象
   
UITouch *touch = touches.anyObject;
   
//拿到当前点击的坐标
   
CGPoint nowLocation = [touch locationInView:self];
   
//拿到上一次的坐标
   
CGPoint lastLocation = [touch previousLocationInView:self];
   
CGFloat subX = nowLocation.x - lastLocation.x;
   
if (subX > 0) {
       
NSLog(@"向右");
    }
else if(subX < 0){
       
NSLog(@"向左");
    }
else if (subX == 0){
       
NSLog(@"垂直");
    }
}

//手指结束滑动时,触发的事件
- (
void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   
NSLog(@"touchesEnded");
}
//手指滑动被中止时
- (
void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
   
NSLog(@"touchesCancelled");
}

//当子视图超过自己的frame 是够剪切
        self.clipsToBounds = YES;


3.运动事件

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
   
NSLog(@"开始晃动");
}

- (
void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
   
NSLog(@"结束晃动");
}

4.事件的传递
iOS—>Application>window>子视图
5.响应者链

 
   
//取到视图控制器 responder转过来
//    UIViewController *rootVC = (UIViewController *)self.nextResponder.nextResponder.nextResponder;
   
UIViewController *motailVC = [[UIViewController alloc] init];
    motailVC.
view.backgroundColor = [UIColor blueColor];
//    [rootVC presentViewController:motailVC animated:YES completion:nil];

//    UIViewController *rootVC = self.viewController;//此处可用点语法点出来
   
//弹出模态视图
//    [rootVC presentViewController:motailVC animated:YES completion:nil];
   
//导航控制器
    [self.viewController.navigationController pushViewController:motailVC animated:YES];

将核心代码写在UIView的类目中
- (UIViewController *)viewController
{
   
//找到视图的下一个响应者
   
UIResponder *nextResponder = self.nextResponder;
   
//利用循环找到响应者
   
do {
       
//如果找到的类是视图控制器就跳出
       
if ([nextResponder isKindOfClass:[UIViewController class]]) {
           
//            break;
           
return (UIViewController *)nextResponder;
        }
       
//如果不是就继续找
        nextResponder = nextResponder.
nextResponder;
       
       
    }
while (nextResponder);//如果找到的响应者有值就一直找
   
   
   
//找不到返回空
   
return nil;
}
6.手势识别器
UIView *gestureView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 300, 300)];
    gestureView.
multipleTouchEnabled = YES;
    gestureView.
backgroundColor = [UIColor grayColor];
    [
self.view addSubview:gestureView];
   
   
/**************点击手势*************/
   
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick1:)];
   
//设置点击的数量
    tap1.
numberOfTapsRequired = 1;
   
//设置手指的个数
    tap1.
numberOfTouchesRequired = 1;
   
//gestureView上添加手势
    [gestureView
addGestureRecognizer:tap1];
   
//双击
   
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick2:)];
    tap2.
numberOfTapsRequired = 2;
    [gestureView
addGestureRecognizer:tap2];
   
   
//如果参数中的手势触发了,则自身失效
    [tap1 requireGestureRecognizerToFail:tap2];

//点击手势
- (
void)tapClick1:(UITapGestureRecognizer *)tap
{
   
NSLog(@"单击");
}
- (
void)tapClick2:(UITapGestureRecognizer *)tap
{
   
NSLog(@"双击");
}

 /**************轻扫手势*************/
   
UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction:)];
   
//设置方向
    swip.
direction = UISwipeGestureRecognizerDirectionDown;
    [gestureView
addGestureRecognizer:swip];
    //轻扫手势
- (
void)swipAction:(UISwipeGestureRecognizer *)swip
{
   
if (swip.direction == UISwipeGestureRecognizerDirectionDown)
    {
        NSLog(@"向下滑动");
    }
   
   
/**************平移手势*************/
   
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [gestureView addGestureRecognizer:pan];
   //平移手势
- (
void)panAction:(UIPanGestureRecognizer *)pan
{
   
CGPoint p = [pan locationInView:pan.view];
   
NSLog(@"%@", NSStringFromCGPoint(p));
}
   
   
    
/**************长按手势*************/
   
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
   
//最少按住时间
    longPress.
minimumPressDuration = 2;
    [gestureView addGestureRecognizer:longPress];
//长按手势
- (
void)longPress:(UILongPressGestureRecognizer *)longpress
{
   
NSLog(@"长按");
}
   
   
/**************旋转手势*************/
   
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(RotationAction:)];
    [gestureView addGestureRecognizer:rotation];

//旋转手势
- (
void)RotationAction:(UIRotationGestureRecognizer *)rotation
{
   
//获取旋转的弧度
   
CGFloat r = rotation.rotation;
   
//弧度转角度
   
NSLog(@"%.2f", 180/M_PI * r);
}
   
   
/**************捏合手势*************/
   
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pintchAction:)];
    [gestureView addGestureRecognizer:pinch];
- (void)pintchAction:(UIPinchGestureRecognizer *)pintch
{
   
//获得捏合的比例
   
CGFloat scale = pintch.scale;
    pintch.
view.transform = CGAffineTransformMakeScale(scale, scale);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值