UITouch 的使用

直接上代码:

touch 的四大状态。:
//
//  TouchView.m
//  UI_practice_04
//
//  Created by lanouhn on 15/4/22.
//  Copyright (c) 2015年 huangyankai. All rights reserved.
//

#import "TouchView.h"
//延展
@interface TouchView ()
@property (nonatomic,assign) CGPoint startPoint;//记录当前起始点坐标

@end

@implementation TouchView
//方法是功能片段的封装。
//触摸開始  表示手指接触到屏幕时,会触发
//图层

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s",__FUNCTION__);
//    CGcolor还有一个框架   给UIColor发一个消息,转化为CGColor 设置阴影
//    设置阴影的颜色
    self.layer.shadowColor = [[UIColor lightGrayColor] CGColor];
//    设置阴影的偏移量
    self.layer.shadowOffset = CGSizeMake(10, 10);
//    设置阴影不透明度
    self.layer.shadowOpacity = 0.9;
//    角半径
    self.layer.cornerRadius = 100;

    UITouch *aTouch = [touches anyObject];
//    获取触摸開始点并保存
    self.startPoint = [aTouch locationInView:self.superview];
}

//触摸移动,当手指触摸屏幕并产生移动的时候,会反复调用此方法
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s",__FUNCTION__);
    UITouch *aTouch = [touches anyObject];
//    父视图上的一个点
    CGPoint currentPoint = [aTouch locationInView:self.superview];
//    通过移动产生的当前触摸点和上一个触摸点的X和Y轴的增量
    CGFloat delta_x = currentPoint.x - self.startPoint.x;
    CGFloat delta_y = currentPoint.y - self.startPoint.y;
//    通过视图当前起始点坐标以及delta_x和delta_y来推算新的起始点坐标
    CGRect frame = self.frame;
    frame.origin.x += delta_x;
    frame.origin.y += delta_y;
    self.frame = frame;
//   将当前点保存下来。成为下一次移动产生的新的点的计算增量根据
    self.startPoint = currentPoint;



}

//触摸结束。表示手指离开屏幕时触发
//NSSet集合,无序性
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s",__FUNCTION__);
    self.layer.shadowColor = nil;
    self.layer.shadowOffset = CGSizeZero;
    self.layer.shadowOpacity = 0;

}
//触摸取消,表示此时手指并未离开屏幕,可是因为一些打断(来电话)而失去与该视图的交互
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s",__FUNCTION__);
}
@end
原文地址:https://www.cnblogs.com/yjbjingcha/p/7116547.html