2012-11-03 26 views
0

移動的UIImageViews在我的滾動視圖IM具有多imageviews在那裏我可以能夠當用戶點擊長按移動的每個imageviews我一直在使用這樣做,,滾動型

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我需要重新排列視圖 ie ie Gallery view。

回答

1

爲了獲得重排行爲,您需要有一些邏輯來設置網格並將元素「捕捉」到元素中,並在需要時將元素移出,等等。這相當複雜。你目前的代碼只是用你的手勢移動視圖。

但是,如果您能夠定位iOS 6及更高版本,則幾乎可以肯定要使用UICollectionView,這可免費爲您提供大量此功能。集合視圖簡化了重新排列後的佈局和邏輯,並且通常會使您的生活變得更加容易批次