2011-04-26 33 views
0

在我的應用程序,我想移動一個子視圖,我彈出。這是使用UIPanGestureRecognizer和手勢識別功能提供的蘋果in this linkUIPanGestureRecognizer在iPad中的問題

所以我得到的問題是,當我點擊按鈕圖像,並嘗試移動視圖,它不會移動視圖。該功能僅在單擊按鈕時起作用,然後單擊並移動它。只有這樣它才能移動視圖。

我想知道我在做什麼錯。

這裏是按鈕的代碼,我添加此功能

UIButton *moveButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 1, 30, 30)]; 
[moveButton addTarget:self   action:@selector(moveButtonClick:)forControlEvents:UIControlEventTouchDown]; 
[moveButton setBackgroundImage:[UIImage imageNamed: @"moveButton.png"] forState:UIControlStateNormal]; 
[customView addSubview:moveButton]; 
[moveButton release]; 

,這裏是我使用的應用程序代碼來識別移動手勢

-(void) moveButtonClick: (id) sender 
{ 
[self addGestureRecognizersToPiece:self.view]; 
} 

// shift the piece's center by the pan amount 
// reset the gesture recognizer's translation to {0, 0} after applying so the next callback is a delta from the current position 
- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer 
{ 
UIView *piece = [gestureRecognizer view]; 

[self adjustAnchorPointForGestureRecognizer:gestureRecognizer]; 

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) { 
    CGPoint translation = [gestureRecognizer translationInView:[piece superview]]; 

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

// adds a set of gesture recognizers to one of our piece subviews 
- (void)addGestureRecognizersToPiece:(UIView *)piece 
{ 
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)]; 
[panGesture setDelegate:self]; 
[panGesture setMaximumNumberOfTouches:1]; 
[piece addGestureRecognizer:panGesture]; 
[panGesture release]; 

} 


// scale and rotation transforms are applied relative to the layer's anchor point 
// this method moves a gesture recognizer's view's anchor point between the user's fingers 
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
{ 
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) 
{ 
    UIView *piece = gestureRecognizer.view; 
    CGPoint locationInView = [gestureRecognizer locationInView:piece]; 
    CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview]; 

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

// UIMenuController requires that we can become first responder or it won't display 
- (BOOL)canBecomeFirstResponder 
{ 
return YES; 
} 

這將是,如果真的很棒有人可以幫我解決這個問題。

更新:解決問題。看看下面提供的答案。

回答

0

我解決了這個問題......顯然使用按鈕來執行我需要的操作,確實需要我點擊兩次,然後才執行平移操作。所以我刪除它並添加了一個圖像並添加了手勢識別器。

這裏是更新的代碼...對於

代碼的圖片 -

// Image which allows subview to be moved around 
CGRect myImageRect = CGRectMake(5, 1, 30, 30); 
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect]; 
[myImage setImage:[UIImage imageNamed:@"moveButton.png"]]; 
myImage.opaque = YES; 
myImage.userInteractionEnabled = YES; 
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self  action:@selector(panPiece:)]; 
[panGesture setDelegate:self]; 
[panGesture setMaximumNumberOfTouches:1]; 
UIView *piece = self.view; 
[piece addGestureRecognizer:panGesture]; 
[panGesture release]; 
[customView addSubview:myImage]; 
[myImage release]; 

在此之後我刪除了按鈕的代碼和刪除的動作識別器的按鈕..除此之外,其餘的代碼是相同的,它工作正常...