触摸方法
2015-08-13 10:03
183 查看
#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__);
}
- (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__);
}
相关文章推荐
- PHP 常用的代码片段
- 浅谈CSS选择器中的空格
- 小白学开发(iOS)OC_ block数据类型(2015-08-08)
- 黑马程序员--java技术blog---第七篇:GUI
- 重构真的能提高代码质量吗?
- poj 1743 Musical Theme
- 鱼和熊掌都要吧!
- HDU 5378 (2015多校第七场1010) 概率DP
- 滑动文本标签,一个类解决广告轮播 UIScrollView
- hdu 3416 Marriage Match IV (最短路+最大流)
- java 导出word乱码问题,99%解决问题
- C语言:内存的分配与管理
- (转载)数据库出现ORA-00283/ORA-01610的问题
- C/C++之类型强制转化
- [POI2007][BZOJ1103] 大都市meg|dfs序|树状数组
- JAVA 设计模式-单例模式
- Gson 转换树型结构的实例
- android内部存储外部存储以及assets文件的操作一些操作
- phpstrom快捷键
- 小白学开发(iOS)OC_ 使用继承来扩充类(2015-08-07)