The ultimate goal of these event paths is to find an object that can handle and respond to an event. Therefore, UIKit first sends the event to the object that is best suited to handle the event. For touch events, that object is the hit-test view, and for other events, that object is the first responder.
The hit-test view is given the first opportunity to handle a touch event. If the hit-test view cannot handle an event, the event travels up that view’s chain of responders as described in “ The Responder Chain Is Made Up of Responder Objects ” until the system finds an object that can handle it.
//具体功能:
//1.每个视图都可以经过触摸拖动,并且在触摸时会有方法的动画效果在(touchesBegin方法中实现)。
//2.当触摸结束是会有放大还原的动画效果 在(touchesEnd方法中实现)。
//3.当触摸其中任意一个视图时,该视图会随着手势一起移动,并且当移动视图的中心点(view.center)进入其他两个视图的frame中时,会将两个视图吸附进来,最后看上去只剩下一个视图 在(touchesMove方法中实现)。
//4.当双击父视图的任意空白处时,三个视图将以动画的效果回到起始位置 在(touchesBegin方法中实现)
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *magentaView;
@property (weak, nonatomic) IBOutlet UIImageView *cyanView;
@property (weak, nonatomic) IBOutlet UIImageView *yellowView;
@end
#define kMagentaViewFrame (CGRect){53,55,100,100}
#define kCyanViewFrame (CGRect){132,254,100,100}
#define kYellowViewFrame (CGRect){223,457,100,100}
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
//自定义方法,求出触摸点的位置返回location
- (CGPoint)locationInTouches:(NSSet *)touches
{
// 0. 先取出触摸
UITouch *touch = [touches anyObject];
// 1. 返回触摸点(注意:坐标系 self.view)
return [touch locationInView:self.view];
}
//自定义方法,判断触摸点是否在三个视图的frame中,是返回YES,该视图随着手势一起移动。
- (BOOL)isImageViewsContainsPoint:(CGPoint)location
{
if (CGRectContainsPoint(_magentaView.frame, location)
|| CGRectContainsPoint(_cyanView.frame, location)
|| CGRectContainsPoint(_yellowView.frame, location)) {
return YES;
}
return NO;
}
//自定义方法,当进行双击时,三个视图回到原来的位置
- (void)resetFrames
{
[UIView animateWithDuration:0.5 animations:^{
_cyanView.frame = kCyanViewFrame;
_magentaView.frame = kMagentaViewFrame;
_yellowView.frame = kYellowViewFrame;
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint location = [self locationInTouches:touches];
// 0. 先取出触摸
UITouch *touch = [touches anyObject];
if ([self isImageViewsContainsPoint:location]) { // 触摸发生在三个视图里
// 1. 取出触摸发生的视图
UIImageView *view = (UIImageView *)touch.view;
// 2. 设置触摸的视图的中心点为触摸点并放大触摸的视图
[UIView animateWithDuration:0.5 animations:^{
view.center = location;
view.transform = CGAffineTransformMakeScale(1.2, 1.2);
}];
} else { // 执行双击操作
if (touch.tapCount == 2) { // 双击
[self resetFrames];
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint location = [self locationInTouches:touches];
// 移动时的触摸点落在哪个视图的矩形区域内,那个视图的中心点就设置为触摸点
if (CGRectContainsPoint(_magentaView.frame, location)) {
[UIView animateWithDuration:0.3 animations:^{
_magentaView.center = location;
}];
}
if (CGRectContainsPoint(_cyanView.frame, location)) {
[UIView animateWithDuration:0.3 animations:^{
_cyanView.center = location;
}];
}
if (CGRectContainsPoint(_yellowView.frame, location)) {
[UIView animateWithDuration:0.3 animations:^{
_yellowView.center = location;
}];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// 0. 先取出触摸
UITouch *touch = [touches anyObject];
// 1. 取出触摸发生的视图
UIImageView *view = (UIImageView *)touch.view;
// 还原触摸的视图
[UIView animateWithDuration:0.5 animations:^{
view.transform = CGAffineTransformIdentity;
}];
}
@end
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/wow09_1225/article/details/47058141