在ScrollView中移动UIImageViews

时间:2012-11-03 06:55:16

标签: objective-c

在我的滚动视图中我有多个图像视图,我可以在用户点击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正在移动我需要重新排列视图的每个地方,例如图库视图。

1 个答案:

答案 0 :(得分:1)

为了获得重新排列的行为,您需要拥有一些设置网格的逻辑,然后才能实现重组。它的元素,在需要时将元素移出等等。它相当复杂。您当前的代码只是用手势移动视图。

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