首先添加拖动手势
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
[mineView setUserInteractionEnabled:YES];//开启图片控件的用户交互
[mineView addGestureRecognizer:pan];//给图片添加手势
在拖拽方法里面执行:
CGFloat KWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat KHeight = [UIScreen mainScreen].bounds.size.height;
//返回在横坐标上、纵坐标上拖动了多少像素
CGPoint point=[rec translationInView:self];
NSLog(@"%f,%f",point.x,point.y);
CGFloat centerX = rec.view.center.x+point.x;
CGFloat centerY = rec.view.center.y+point.y;
CGFloat viewHalfH = rec.view.frame.size.height/2;
CGFloat viewhalfW = rec.view.frame.size.width/2;
//确定特殊的centerY
if (centerY - viewHalfH < 0 ) {
centerY = viewHalfH;
}
if (centerY + viewHalfH > KHeight ) {
centerY = KHeight - viewHalfH;
}
//确定特殊的centerX
if (centerX - viewhalfW < 0){
centerX = viewhalfW;
}
if (centerX + viewhalfW > KWidth){
centerX = KWidth - viewhalfW;
}
rec.view.center = CGPointMake(centerX, centerY);
//拖动完之后,每次都要用setTranslation:方法制0这样才不至于不受控制般滑动出视图
[rec setTranslation:CGPointMake(0, 0) inView:self];
可实现在屏幕内部拖拽且不脱离屏幕外部。
下面实现类似微信两人视频对话时,对话框拖拽的效果(增加重力动画):
- (void)handlePan:(UIPanGestureRecognizer *)rec {
CGFloat KWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat KHeight = [UIScreen mainScreen].bounds.size.height;
//返回在横坐标上、纵坐标上拖动了多少像素
CGPoint point=[rec translationInView:self];
NSLog(@"%f,%f",point.x,point.y);
CGFloat endX = rec.view.center.x+point.x;
CGFloat endY = rec.view.center.y+point.y;
CGFloat viewHalfH = rec.view.frame.size.height/2;
CGFloat viewhalfW = rec.view.frame.size.width/2;
if (rec.state == UIGestureRecognizerStateEnded) {
//计算距离最近的边缘 吸附到边缘停靠
CGFloat topRange = endY;//上距离
CGFloat bottomRange = KHeight - endY;//下距离
CGFloat leftRange = endX;//左距离
CGFloat rightRange = KWidth - endX;//右距离
//比较上下左右距离 取出最小值
CGFloat minRangeTB = topRange > bottomRange ? bottomRange : topRange;//获取上下最小距离
CGFloat minRangeLR = leftRange > rightRange ? rightRange : leftRange;//获取左右最小距离
CGFloat minRange = minRangeTB > minRangeLR ? minRangeLR : minRangeTB;//获取最小距离
//判断最小距离属于上下左右哪个方向 并设置该方向边缘的point属性
CGPoint minPoint;
if (minRange == topRange) {
//上
endX = endX - viewhalfW < 0 ? viewhalfW : endX;
endX = endX + viewhalfW > KWidth ? KWidth - viewhalfW : endX;
minPoint = CGPointMake(endX , 0 + viewHalfH);
} else if(minRange == bottomRange){
//下
endX = endX - viewhalfW < 0 ? viewhalfW : endX;
endX = endX + viewhalfW > KWidth ? KWidth - viewhalfW : endX;
minPoint = CGPointMake(endX , KHeight - viewHalfH);
} else if(minRange == leftRange){
//左
endY = endY - viewHalfH < 0 ? viewHalfH : endY;
endY = endY + viewHalfH > KHeight ? KHeight - viewHalfH : endY;
minPoint = CGPointMake(0 + viewhalfW , endY);
} else {
//右
endY = endY - viewHalfH < 0 ? viewHalfH : endY;
endY = endY + viewHalfH > KHeight ? KHeight - viewHalfH : endY;
minPoint = CGPointMake(KWidth - viewhalfW , endY);
}
// 不加动画的话,显示下面这个
// rec.view.center = minPoint;
//添加吸附物理行为
UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.mineView attachedToAnchor:minPoint];
[attachmentBehavior setLength:0];
[attachmentBehavior setDamping:0.1];
[attachmentBehavior setFrequency:5];
[self.animator addBehavior:attachmentBehavior];
}else {
// 移除之前的所有行为
[self.animator removeAllBehaviors];
rec.view.center = CGPointMake(endX, endY);
}
//拖动完之后,每次都要用setTranslation:方法制0这样才不至于不受控制般滑动出视图
[rec setTranslation:CGPointMake(0, 0) inView:self];
}
网友评论