触摸方法

    • #pragma mark 开始触摸
    • - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    • {
    • // 获取第一个触摸的点
    • UITouch *touch = [touches anyObject];
    • // 获取这点相对于window坐标
    • CGPoint point = [touch locationInView:self.window];
    • // 将x轴与y轴差值赋值给实例变量
    • _x = point.x - self.center.x;
    • _y = point.y - self.center.y;
    • }
    • #pragma mark 触摸过程中
    • - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    • {
    • // 1. 在集合中取出触摸的点
    • UITouch *touch = [touches anyObject];
    • // 2. 获取这个点坐标信息  (针对window或者自身)
    • CGPoint point = [touch locationInView:self.window];
    • NSLog(@"%@", NSStringFromCGPoint(point));
    • CGFloat x = point.x - _x;
    • CGFloat y = point.y - _y;
    • self.center = CGPointMake(x, y);    // 中心点坐标
    • }
    • #pragma mark 触摸结束
    • - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    • {
    • NSLog(@"%d %s", __LINE__, __FUNCTION__);
    • CGFloat red = (arc4random() % 256) / 255.0;
    • CGFloat green = (arc4random() % 256) / 255.0;
    • CGFloat blue = (arc4random() % 256) / 255.0;
    • self.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
    • }
    • #pragma mark 触摸被取消
    • - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    • {
    • NSLog(@"%d %s", __LINE__, __FUNCTION__);
    • }
时间: 2024-08-08 22:19:47

触摸方法的相关文章

【iOS开发-触摸】移动的UIView例子

iOS触摸 方法: //开始接触 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; - //在屏幕上移动 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; - //触摸结束 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; - //取消触摸 - (voi

iOS_39_触摸解锁

最终效果图: 控制器: // // BeyondViewController.m // 39_触摸解锁 // // Created by beyond on 14-9-17. // Copyright (c) 2014年 com.beyond. All rights reserved. // #import "BeyondViewController.h" #import "LockView.h" #import "LockViewDelegate.h&q

009-手势触摸事件处理

问题一:iOS中有哪些事件? •在用户使用app过程中,会产生各种各样的事件 •iOS中的事件可以分为3大类型 问题二:什么叫响应者对象? •在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.我们称之为“响应者对象” •UIApplication.UIViewController.UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件 问题三:UIResponder内部提供了哪些处理事件的方法来处理事件? •UIRes

iOS中创建数组的简易方法与手势的使用

其实做外包也是挺不错,虽然累点,但是一天天的知识的扩展不少,下面是今天的收获 ①初始化数组一字典的方法 @[] 初始化不可变数组 @{} 初始化不可变字典 ②手势的使用 iOS中处理手势之前是用四个方法: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event - (void)touch

iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控

-- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事件(手势操作).运动事件.远程控制事件等展开学习: iOS事件简介 触摸事件 手势识别 运动事件 远程控制事件 iOS事件 在iOS中事件分为三类: 触摸事件:通过触摸.手势进行触发(例如手指点击.缩放) 运动事件:通过加速器进行触发(例如手机晃动) 远程控制事件:通过其他远程设备触发(例如耳机控制

iOS开发笔记--iOS中的触摸事件和手势处理

iOS中的事件可以分为三大类:原文:http://my.oschina.net/aofe/blog/268749 1> 触摸事件 2> 加速计事件 3> 远程控制事件 响应者对象 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.我们称之为"响应者对象". UIApplication,UIViewController,UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件. UIRespon

触摸事件,手势识别

•在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.我们称之为“响应者对象” • •UIApplication.UIViewController.UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件 •UIResponder内部提供了以下方法来处理事件 Ø触摸事件 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; - (void)tou

UI进阶--触摸事件

在iOS中,触摸事件分为3种类型: 1.触摸事件:Multitouch events 2.加速计事件:Accelerometer events 3.远程事件:Remote control events 响应者对象: 在iOS中,只有继承了UIResponder的对象能接收并处理事件的,UIApplication.UIViewController.UIView都继承自UIResponder都是响应者对象. UIResponder内部提供了以下方法来处理事件 触摸事件: 1 - (void)touc

iOS中的触摸事件

1.响应者对象 iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件,我们称之为“响应者对象”. UIApplication.UIViewController.UIView都继承自UIResponder,因此他们都是响应者对象,都能够接收并处理事件. 2.UIResponde UIResponder内部提供了以下方法来处理事件 触摸事件: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)e