IOS开发之——事件处理-View拖动
<p>文章搬运来源:https://blog.csdn.net/Calvin_zhou/article/details/110931665<br>作者:PGzxc(如有侵权,联系作者,立即删除)</p>
<p><strong>对iOS开发感兴趣,可以看一下作者的iOS交流群:812157648,大家可以在里面吹水、交流相关方面的知识,群里还有我整理的有关于面试的一些资料,欢迎大家加群,大家一起开车</strong></p>
<h2 id="一-ios中的事件">一 IOS中的事件</h2>
<ul>
<li>在用户使用app过程中,会产生各种各样的事件</li>
<li>IOS中的事件可以分为3大类型:触摸事件、加速计事件、远程控制事件</li>
</ul>
<h2 id="二-响应者对象">二 响应者对象</h2>
<ul>
<li>在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件。我们称之为“响应者对象”</li>
<li>UIApplication、UIViewController、UIView都能继承自UIResponder,因此它们都是响应者对象,都能接收并处理事件</li>
</ul>
<h2 id="三-uiresponder">三 UIResponder</h2>
<h3 id="31-uiresponder内部提供了以下方法来处理事件">3.1 UIResponder内部提供了以下方法来处理事件</h3>
<h4 id="触摸事件">触摸事件</h4>
<pre><code>-(void)touchBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchMoved:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchEnded:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
</code></pre>
<h4 id="加速计事件">加速计事件</h4>
<pre><code>-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
</code></pre>
<h4 id="远程控制事件">远程控制事件</h4>
<pre><code>-(void)remoteControlReceivedWithEvent:(UIEvent *)event;
</code></pre>
<h2 id="四-uiview的触摸事件处理">四 UIView的触摸事件处理</h2>
<h3 id="41-uiview是uiresponder的子类可以实现下列4个方法处理不同的触摸事件">4.1 UIView是UIResponder的子类,可以实现下列4个方法处理不同的触摸事件</h3>
<h4 id="一根或者多根手指开始触摸view系统会自动调用view的下面方法">一根或者多根手指开始触摸view,系统会自动调用view的下面方法</h4>
<ul>
<li>-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;</li>
</ul>
<h4 id="一根或多根手指在view上移动系统会自动调用view的下面方法随着手指的移动持续调用该方法">一根或多根手指在view上移动,系统会自动调用view的下面方法(随着手指的移动持续调用该方法)</h4>
<ul>
<li>-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;</li>
</ul>
<h4 id="一根或者多根手指离开view系统会自动调用view的下面方法">一根或者多根手指离开view,系统会自动调用view的下面方法</h4>
<ul>
<li>-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;</li>
</ul>
<h4 id="触摸结束前某个系统事件例如电话呼入会打断触摸过程系统会自动调用下面的方法">触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程,系统会自动调用下面的方法</h4>
<ul>
<li>-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;</li>
</ul>
<h2 id="42-说明">4.2 说明</h2>
<ul>
<li>touches中存放的都是UITouch对象</li>
</ul>
<h2 id="五-uitouch">五 UITouch</h2>
<h3 id="51-什么是uitouch">5.1 什么是UITouch</h3>
<ul>
<li>当用户用一根手指触摸屏幕时,会创建一个与手指相关联的UITouch对象</li>
<li>一根手指对应一个UITouch对象</li>
</ul>
<h3 id="52-uitouch的作用">5.2 UITouch的作用</h3>
<ul>
<li>保存着跟手指相关的信息,比如触摸的位置、时间、阶段</li>
</ul>
<h3 id="53-手指移动时">5.3 手指移动时</h3>
<ul>
<li>当手指移动时,系统会更新同一个UITouch对象,使之能够一直保存该手指的触摸位置</li>
</ul>
<h3 id="54-常用的属性">5.4 常用的属性</h3>
<h4 id="触摸产生时所处的窗口">触摸产生时所处的窗口</h4>
<pre><code>@property(nonatomic,readonly,retain)UIWindow *window;
</code></pre>
<h4 id="触摸产生时所处的视图">触摸产生时所处的视图</h4>
<pre><code>@property(nonatomic,readonly,retain)UIView *view;
</code></pre>
<h4 id="短时间内点击屏幕的次数可以根据tapcount判断单击双击或更多的点击">短时间内点击屏幕的次数,可以根据tapCount判断单击、双击或更多的点击</h4>
<pre><code>@property(nonatomic,readonly)NSUInteger tapCount;
</code></pre>
<h4 id="记录了触摸事件产生或变化的时间单位是秒">记录了触摸事件产生或变化的时间,单位是秒</h4>
<pre><code>@property(nonatomic,readonly)NSTimeInterval timestamp;
</code></pre>
<h4 id="当前触摸事件所处的状态">当前触摸事件所处的状态</h4>
<pre><code>@property(nonatomic,readonly)UITouchPhase phase;
</code></pre>
<h3 id="55-常用的方法">5.5 常用的方法</h3>
<h4 id="touchescount">touches.count</h4>
<ul>
<li>触摸的手指数(勾选Multiple Touch)</li>
</ul>
<h4 id="touchtapcount">touch.tapCount</h4>
<ul>
<li>点击屏幕的次数</li>
</ul>
<h4 id="touchphase枚举类型">touch.phase(枚举类型)</h4>
<pre><code>typedef NS_ENUM(NSInteger, UITouchPhase) {
UITouchPhaseBegan, // whenever a finger touches the surface.
UITouchPhaseMoved, // whenever a finger moves on the surface.
UITouchPhaseStationary, // whenever a finger is touching the surface but hasn't moved since the previous event.
UITouchPhaseEnded, // whenever a finger leaves the surface.
UITouchPhaseCancelled,
</code></pre>
<h2 id="六-view拖动">六 view拖动</h2>
<h3 id="61-思路">6.1 思路</h3>
<ul>
<li>获取当前手指的位置</li>
<li>获取上一个点</li>
<li>计算偏移量(x轴和y轴)</li>
<li>获取视图Center,根据偏移量获取最新位置</li>
</ul>
<h3 id="62-代码">6.2 代码</h3>
<pre><code>//触摸移动
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch=;
//获取当前的位置
CGPoint current=;
CGPoint pre=;
//x轴的偏移量
CGFloat offsetX=current.x-pre.x;
CGFloat offsetY=current.y-pre.y;
CGPoint center=self.center;
center.x+=offsetX;
center.y+=offsetY;
self.center=center;
//NSLog(@"%d",touch.phase);
//NSLog(@"%s---%p",__func__,touch);
}
</code></pre>
<h3 id="63-效果图">6.3 效果图</h3>
<p><img src="https://upload-images.jianshu.io/upload_images/25330009-225ed8cba90142e1.gif?imageMogr2/auto-orient/strip"></p><br><br>
来源:https://www.cnblogs.com/fadaijun/p/14330019.html
頁:
[1]