UIView的視圖拖動實(shí)現(xiàn)可以通過Touch的觸摸事件來實(shí)現(xiàn)摆碉,也可以在UIView中添加拖動手勢來實(shí)現(xiàn).
UITouch
UIView是UIResponder的子類,觸摸的時(shí)候只需要重寫對應(yīng)的觸摸方法即可.
// Generally, all responders which do custom touch handling should override all four of these methods.
// Your responder will receive either touchesEnded:withEvent: or touchesCancelled:withEvent: for each
// touch it is handling (those touches it received in touchesBegan:withEvent:).
// *** You must handle cancelled touches to ensure correct behavior in your application. Failure to
// do so is very likely to lead to incorrect behavior or crashes.
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
- (void)touchesEstimatedPropertiesUpdated:(NSSet<UITouch *> *)touches NS_AVAILABLE_IOS(9_1);
@implementation TouchView
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"FlyElephant---觸摸開始");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
// 獲取上一個點(diǎn)
CGPoint prePoint = [touch previousLocationInView:self];
CGFloat offsetX = currentPoint.x - prePoint.x;
CGFloat offsetY = currentPoint.y - prePoint.y;
NSLog(@"FlyElephant----當(dāng)前位置:%@---之前的位置:%@",NSStringFromCGPoint(currentPoint),NSStringFromCGPoint(prePoint));
self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"FlyElephant---觸摸結(jié)束");
}
@end
TouchView *touchView = [[TouchView alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];
touchView.backgroundColor = [UIColor greenColor];
[self.view addSubview:touchView];
UIPanGestureRecognizer
初始化視圖:
UIView *tagView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
tagView.backgroundColor = [UIColor redColor];
self.tagView = tagView;
[self.view addSubview:self.tagView];
手勢狀態(tài)觸摸的過程中有枚舉狀態(tài)可以判斷:
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible, // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state
UIGestureRecognizerStateBegan, // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateChanged, // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateEnded, // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateCancelled, // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateFailed, // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible
// Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
};
狀態(tài)過程處理:
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
NSLog(@"FlyElephant---視圖拖動開始");
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint location = [recognizer locationInView:self.view];
if (location.y < 0 || location.y > self.view.bounds.size.height) {
return;
}
CGPoint translation = [recognizer translationInView:self.view];
NSLog(@"當(dāng)前視圖在View的位置:%@----平移位置:%@",NSStringFromCGPoint(location),NSStringFromCGPoint(translation));
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointZero inView:self.view];
} else if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled) {
NSLog(@"FlyElephant---視圖拖動結(jié)束");
}
}