关于UI的屏幕触控事件示例

------视图控制器的.m中------------

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

UIView *gestureView = [[UIView alloc]initWithFrame:CGRectMake(20, 50, 335, 400)];

gestureView.backgroundColor = [UIColor purpleColor];

[self.view addSubview:gestureView];

/**********************点击手势***************************/

UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap1Action:)];

//一个点击手势,只能识别一种手势,单击和双击是两种不同的手势

//点击次数

tap1.numberOfTapsRequired = 1;

//点击个数

tap1.numberOfTouchesRequired  =1;

//往gestureView张添加手势

[gestureView addGestureRecognizer:tap1];

UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap2Action:)];

tap2.numberOfTapsRequired = 2;

[gestureView addGestureRecognizer:tap2];

//如果参数中的手势触发了,则自身失效

[tap1 requireGestureRecognizerToFail:tap2];

/**********************轻扫手势**************************/

UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];

//设置方向

swip.direction = UISwipeGestureRecognizerDirectionLeft;

[gestureView addGestureRecognizer:swip];

/**********************平移手势**************************/

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];

[gestureView addGestureRecognizer:pan];

/**********************长按手势**************************/

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

//设置长按的最短时间

longPress.minimumPressDuration = 2;

[gestureView addGestureRecognizer:longPress];

/**********************旋转手势**************************/

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];

[gestureView addGestureRecognizer:rotation];

/**********************捏合手势**************************/

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];

[gestureView addGestureRecognizer:pinch];

}

- (void)tap1Action:(UITapGestureRecognizer *)tap1{

NSLog(@"单击");

}

- (void)tap2Action:(UITapGestureRecognizer *)tap2{

NSLog(@"双击");

}

- (void)swipAction:(UISwipeGestureRecognizer *)swip{

if (swip.direction == UISwipeGestureRecognizerDirectionLeft) {

NSLog(@"向左轻扫");

}

}

- (void)panAction:(UIPanGestureRecognizer *)pan{

CGPoint point = [pan locationInView:pan.view];

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

}

- (void)longPress:(UILongPressGestureRecognizer *)longPress{

if (longPress.state == UIGestureRecognizerStateBegan) {

NSLog(@"长按开始");

}else if (longPress.state == UIGestureRecognizerStateEnded){

NSLog(@"长按结束");

}

}

- (void)rotation:(UIRotationGestureRecognizer *)rotation{

//获取旋转的角度-----先得到弧度

CGFloat r = rotation.rotation;

//转成角度

//180/PI = x/r

NSLog(@"x is %.2f",180/M_PI*r);

}

- (void)pinch:(UIPinchGestureRecognizer *)pinch{

//获得捏合的比例

CGFloat scale = pinch.scale;

pinch.view.transform  = CGAffineTransformMakeScale(scale, scale);

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

时间: 2024-07-31 21:11:47

关于UI的屏幕触控事件示例的相关文章

Android触控事件

触控事件 MotionEvent类: //单击触摸按下动作 public static final int ACTION_DOWN = 0; /** * Constant for {@link #getActionMasked}: A pressed gesture has finished, the * motion contains the final release location as well as any intermediate * points since the last d

舌尖上的安卓(android触控事件机制学习笔记录)

对于一个"我们从来不生产代码,我们只是大自然代码的搬运工"的码农来说.对android的触控机制一直是模棱两可的状态,特别是当要求一些自定义的控件和androide的自带控件(比如ViewPager,ListView,ScrollView)高度嵌套在一起使用时. 花了点时间梳理了下,做个笔记.对于一个触控的事件从用户输入到传递到Actigvity到最外层的Viewgroup在到子View,中间过程还可能穿插多个Viewgroup,android在ViewGroup提供了3个方法来控制流

微信小程序之触控事件(四)

>>>什么是事件 事件是视图层到逻辑层的通讯方式. 事件可以将用户的行为反馈到逻辑层进行处理. 事件可以绑定在组件上,当达到触发事件,就会执行逻辑层中对应的事件处理函数. 事件对象可以携带额外信息,如id, dataset, touches. >>>事件分类 touchstart 手指触摸 touchmove 手指触摸后移动 touchcancel 手指触摸动作被打断,如弹窗和来电提醒 touchend 手指触摸动作结束 tap 手指触摸后离开 longtap 手指触摸

WPF 中如何屏蔽多点触控事件?

由于项目中还没有更好的多点触控思路,所以需要将多点触控暂时关闭: 关闭多点触控的代码只有一行: ? private void image_ManipulationStarting(object sender, ManipulationStartingEventArgs e) { e.Mode = ManipulationModes.None;  } 留个记录,以备日后用; 但是,ManipulationModes.None并不是乱用的,详情参照MSDN: http://msdn.microsof

移动端触控事件

<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>移动终端</title><script>window.onload=function(){ var startx = 0,i=1; var starty = 0; var endx =

GridView(网格视图)+MotionEvent(触控事件)实现可以拖动排序的网格图

1.一触碰,就回调onInterceptTouchEvent(MotionEvent ev)事件,每次的触碰都只回调一次,接着只要还没有放手,就一直回调onTouchEvent(MotionEvent ev)事件. 2.监听长按点击事件,如果长按点击,则将图片放大,拖动显示的也是这张放大的图片,在onTouchEvent(MotionEvent ev)中判断MotionEvent 事件的类型,如果是拖动操作,则根据拖动的位置,用windowManager.updateViewLayout(dra

〖Android〗屏幕触屏事件录制与回放

需求: 不管是做自动化测试的,还是传媒技术的,自动化操作Android App是一种操作需求: 自动化的操作可以节省很多的人力资源投入: 实现: Android UI界面的自动化,通常有两个方法: 1. 使用得较多的是input命令,input可以直接输入“所有按键.屏幕点击.拖动和直接输入文本内容”: 2. 较为复杂的操作使用getevent 和 sendevent来完成,理论上可以操作一切触屏的输入: 问题: 通常,input使用起来会特别简单方便,可以把坐标记录好,写写脚本就完事了: 但是

手指多点触控事件

1 package com.itheima.touch; 2 3 import android.app.Activity; 4 import android.graphics.Matrix; 5 import android.graphics.PointF; 6 import android.os.Bundle; 7 import android.view.MotionEvent; 8 import android.view.View; 9 import android.view.View.On

Android触控屏幕Gesture(GestureDetector和SimpleOnGestureListener的使用教程)

1.当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(View v, MotionEvent event)方法,我们可以处理一些touch事件,但是这个方法太过简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦(因为我们要自己根据用户触摸的轨迹去判断是什么手势)Android sdk给我们提供了GestureDetector(Gesture:手势Dete