这个要是实现的效果就是当滚动视图向左右拖拽的时候是正常的,但是如果将图片向下拉的时候就会将这个图片拖拽出来,看效果图:这个上面的图就是在滚动视图里,下面的图就拖拽上面的图的时候生成的图片。
关键代码粘贴在下面,要实现这样的效果我们要实现自定义的UIImageView ,打开UIImageView的交互,添加拖动手势:
- (instancetype)initWithImage:(UIImage *)image
{
if (self = [super initWithImage:image]) {
self.userInteractionEnabled = YES;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
pan.delegate = self;
[self addGestureRecognizer:pan];
}
return self;
}
接下来就是要来判断手势的滑动的方向,我们这里是判断当向下滑动的时候才能够拖拽出来:
float dx = DX(touchLocation, startPoint);
float dy = DY(touchLocation, startPoint);
BOOL finished = YES;
if ((dx > kSwipeDragMin) && (ABS(dy) < kDragLimitMax)) {
touchType = TouchSwipeLeft;
}else if ((-dx > kSwipeDragMin) && (ABS(dy) < kDragLimitMax)){
touchType = TouchSwipeRight;
}else if ((dy > kSwipeDragMin) && (ABS(dx) < kDragLimitMax)){
touchType = TouchSwipeUp;
}else if ((-dy > kSwipeDragMin) && (ABS(dx) < kDragLimitMax)){
touchType = TouchSwipeDown;
}else{
finished = NO;
}
当判断出滑动的动作是向下的,就要创建一个这张图片的对象,将其拖拽出来:
if (!gestureWasHandled && finished &&(touchType == TouchSwipeDown)) {
dragView = [[DragView alloc] initWithImage:self.image];
dragView.center = touchLocation;
[supersuper addSubview: dragView];
scrollView.scrollEnabled = NO;
gestureWasHandled = YES;
}
在这里新创建的对象也要添加拖拽的手势,这样才能够进行拖拽。