直接上代码:
touch 的四大状态。:
#import "TouchView.h"
@interface TouchView ()
@property (nonatomic,assign) CGPoint startPoint;
@end
@implementation TouchView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"%s",__FUNCTION__);
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];
CGFloat delta_x = currentPoint.x - self.startPoint.x;
CGFloat delta_y = currentPoint.y - self.startPoint.y;
CGRect frame = self.frame;
frame.origin.x += delta_x;
frame.origin.y += delta_y;
self.frame = frame;
self.startPoint = currentPoint;
}
- (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