Objective c 在ScrollView中移动UIImageView
在我的scrollview中,我有多个ImageView,当用户点击LONGPRESS时,我可以移动每个ImageView。我使用Objective c 在ScrollView中移动UIImageView,objective-c,Objective C,在我的scrollview中,我有多个ImageView,当用户点击LONGPRESS时,我可以移动每个ImageView。我使用 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveImage:)]; [panGesture setMaximumNumberOfTouches:2]; [panGesture setDelegat
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中到处移动,我需要重新排列视图,就像Gallery view一样。要获得这种重新排列行为,你需要有一些逻辑来设置网格并“捕捉”元素,在需要时移动元素,等等。它相当复杂。您当前的代码只是用手势移动视图 然而,如果你能够瞄准iOS 6及以上版本,你几乎肯定会想使用
UICollectionView
,这会让你免费获得很多。集合视图简化了重新安排后的布局和逻辑,通常会使您的生活更加轻松