UITabView内部元素拖动
1加手势识别器
- (void)longPressGestureRecognized:(id)sender{
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
UIGestureRecognizerState longPressState = longPress.state;
//手指在tableView中的位置
_fingerLocation = [longPress locationInView:self];
//手指按住位置对应的indexPath,可能为nil
_relocatedIndexPath = [self indexPathForRowAtPoint:_fingerLocation];
switch (longPressState) {
case UIGestureRecognizerStateBegan:{
//手势开始,对被选中cell截图,隐藏原cell
break;
}
case UIGestureRecognizerStateChanged:{
//点击位置移动,判断手指按住位置是否进入其它indexPath范围,若进入则更新数据源并移动cell
//必须更具位置动态更新数据源
break;
}
default: {
//长按手势结束或被取消,移除截图,显示cell
break;
}
}
}
2将cell截图
/** 返回一个给定view的截图. */
- (UIView *)customSnapshotFromView:(UIView *)inputView {
// Make an image from the input view.
UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0);
[inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Create an image view.
UIView *snapshot = [[UIImageView alloc] initWithImage:image];
snapshot.center = inputView.center;
snapshot.layer.masksToBounds = NO;
snapshot.layer.cornerRadius = 0.0;
snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);
snapshot.layer.shadowRadius = 5.0;
snapshot.layer.shadowOpacity = 0.4;
return snapshot;
}
3定时器score位置更新
- (void)startAutoScroll{
CGFloat pixelSpeed = 4;
if (_autoScrollDirection == PKSnapshotMeetsEdgeTop) {//向下滚动
if (self.contentOffset.y > 0) {//向下滚动最大范围限制
[self setContentOffset:CGPointMake(0, self.contentOffset.y - pixelSpeed)];
_snapshot.center = CGPointMake(_snapshot.center.x, _snapshot.center.y - pixelSpeed);
}
}else{ //向上滚动
if (self.contentOffset.y + self.bounds.size.height < self.contentSize.height) {//向下滚动最大范围限制
[self setContentOffset:CGPointMake(0, self.contentOffset.y + pixelSpeed)];
_snapshot.center = CGPointMake(_snapshot.center.x, _snapshot.center.y + pixelSpeed);
}
}
/* 当把截图拖动到边缘,开始自动滚动,如果这时手指完全不动,则不会触发‘UIGestureRecognizerStateChanged’,对应的代码就不会执行,导致虽然截图在tableView中的位置变了,但并没有移动那个隐藏的cell,用下面代码可解决此问题,cell会随着截图的移动而移动
*/
_relocatedIndexPath = [self indexPathForRowAtPoint:_snapshot.center];
if (_relocatedIndexPath && ![_relocatedIndexPath isEqual:_originalIndexPath]) {
[self cellRelocatedToNewIndexPath:_relocatedIndexPath];
}
}
4结束后处理数据