0

在我的滚动视图中,我有多个图像视图,当用户点击 LONGPRESS 时,我可以移动每个图像视图,我已经这样做了,

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveImage:)];
[panGesture setMaximumNumberOfTouches:2];
[panGesture setDelegate:self];
[imageview addGestureRecognizer:panGesture];


UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] init];
[imageview addGestureRecognizer:longPressRecognizer];

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

return YES;
}



- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizerv     {
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
    UIView *piece = gestureRecognizerv.view;
    CGPoint locationInView = [gestureRecognizerv locationInView:piece];
    CGPoint locationInSuperview = [gestureRecognizerv locationInView:piece.superview];

    piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
    piece.center = locationInSuperview;
}
}


- (void)moveImage:(UIPanGestureRecognizer *)gestureRecognizerg
{
NSLog(@"sdfgdsgsdg");
UIView *piece = [gestureRecognizerg view];
[self adjustAnchorPointForGestureRecognizer:gestureRecognizerg];

// need to test if the scrollview is already using the touches. If it is, leave them
if (!galleryView.tracking) {
    if ([gestureRecognizerg state] == UIGestureRecognizerStateBegan || [gestureRecognizerg state] == UIGestureRecognizerStateChanged) {
        CGPoint translation = [gestureRecognizerg translationInView:[piece superview]];

        [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
        [gestureRecognizerg setTranslation:CGPointZero inView:[piece superview]];
    }
}
}

在这里,我的问题是IMAGEVIEW 正在移动 scollview 中的每个位置,我需要重新排列视图,例如画廊视图。

4

1 回答 1

1

要获得这种重新排列行为,您将需要一些逻辑来设置网格并将元素“捕捉”到它,在需要时将元素移开等等。这相当复杂。您当前的代码只是用您的手势移动视图。

但是,如果您能够以 iOS 6 及更高版本为目标,您几乎肯定会想要使用 aUICollectionView来代替,它可以免费为您提供很多这样的功能。集合视图简化了重新排列背后的布局和逻辑,通常会让你的生活轻松。

于 2012-11-03T09:34:52.840 回答