2011-04-11 188 views
5

我試圖拖動圖像,並在釋放它之後讓它返回原始位置。 (如在回答看到了這個問題::Basic Drag and Drop in iOS)到目前爲止,我可以使用下面的代碼創建它作爲一個按鈕,拖動圖像拖放對象並將它們返回到原始位置

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button addTarget:self action:@selector(imageTouch:withEvent:) forControlEvents:UIControlEventTouchDown]; 
[button addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside]; 
[button setImage:[UIImage imageNamed:@"vehicle.png"] forState:UIControlStateNormal]; 
[self.view addSubview:button]; 

後來我定義:

- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event 
{ 
    CGPoint point = [[[event allTouches] anyObject] locationInView:self.view]; 
    UIControl *control = sender; 
    control.center = point; 
} 

我怎麼才能讓它在釋放它之後回到它原來的位置呢?對於初學者,我將保存每個圖像應該返回的位置,但是,無論如何,指示UIImage從它的當前位置移動並移動到另一個?或者其他的替代解決方案?

回答

-1

你知道嗎,我只是工作這種昨天,所以在這裏你去一點點獎金動畫:

- (void)previewImageTouchUpInside:(UIButton*)aButton { 
    if (!previewImageDragged) { 
     [self selectPreviewImage:aButton]; 
     return; 
} 

    previewImageDragged = NO; 

    // If user drag photo a little and then release, we'll still treat it as a tap 
    // instead of drag 
    if ((originalPositionOfButton.x - aButton.center.x) * 
     (originalPositionOfButton.x - aButton.center.x) + 
     (originalPositionOfButton.y - aButton.center.y) * 
     (originalPositionOfButton.y - aButton.center.y) < 10) { 
     aButton.center = originalPositionOfButton; 
     [self selectPreviewImage:aButton]; 
     return; 
    } 

    [UIView animateWithDuration:0.5 
     delay:0 
     options:UIViewAnimationOptionCurveEaseOut | 
     UIViewAnimationOptionAllowUserInteraction animations:^{ 
     aButton.center = originalPositionOfButton; 
    } completion:nil]; 
} 



- (void)previewImageDraggedInside:(UIButton*)aButton event:(UIEvent*)event { 
    if (!previewImageDragged) { 
     originalPositionOfButton = aButton.center; 
     [self.view bringSubviewToFront:aButton]; 
     [self.view bringSubviewToFront:[self.view viewWithTag:CHOOSE_PHOTO_BUTTON_TAG]]; 
    } 

    UITouch* touch = [[event allTouches] anyObject]; 

    previewImageDragged = YES; 
    aButton.center = CGPointMake(aButton.center.x+[touch locationInView:self.view].x- 
     [touch previousLocationInView:self.view].x, 
     aButton.center.y+[touch locationInView:self.view].y- 
     [touch previousLocationInView:self.view].y); 
} 

它仍然有一些神奇數字,我還沒有改名的函數和變量以適應你的例子,但它應該很清楚。我也在這裏做了縮進,因爲我不會在代碼中手動分解長行。

0

如何調用UIControlEventTouchUpInside上的選擇器,然後在選擇器中,將按鈕的框架設置爲新的x和y座標(已保存的座標)。

+0

謝謝。但那會立即將UIButton移動到一個新的位置,對嗎?如果是這樣,一旦UIControlEventTouchUpInside上的選擇器被調用,可以命令UIButton從它的當前位置移動到新的位置? – 2011-04-11 22:23:12

+0

閱讀「動畫」 – 2011-04-12 03:13:28

7

蘋果推薦UIGestureRecognizer用於最新的iOS。你不僅可以將它用於UIButton,也可以用於UIView(尤其是UIImageView)等等,所以我想推薦它。

在接口:

@interface TestDragViewController : UIViewController { 
    IBOutlet UIImageView *dragImage; 
    CGPoint originalCenter; 
} 

在viewDidLoad中,PLZ記得讓用戶交互:

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self 
                       action:@selector(dragGesture:)]; 
    [dragImage addGestureRecognizer:panGesture]; 
    [dragImage setUserInteractionEnabled:YES]; 
} 

,並在其選擇,我只設置動畫以可視化的恢復效果:

#pragma mark - 
#pragma mark UIPanGestureRecognizer selector 
- (void) dragGesture:(UIPanGestureRecognizer *) panGesture{ 
    CGPoint translation = [panGesture translationInView:self.view]; 

    switch (panGesture.state) { 
     case UIGestureRecognizerStateBegan:{ 
      originalCenter = dragImage.center; 
     } 
      break; 
     case UIGestureRecognizerStateChanged:{ 
      dragImage.center = CGPointMake(dragImage.center.x + translation.x, 
              dragImage.center.y + translation.y); 
     } 
      break; 
     case UIGestureRecognizerStateEnded:{    
      [UIView animateWithDuration:kImageReturnTime 
          animations:^{ 
           dragImage.center = originalCenter; 
          } 
          completion:^(BOOL finished){ 
           NSLog(@"Returned"); 
          }]; 
     } 
      break; 
     default: 
      break; 
    } 

    [panGesture setTranslation:CGPointZero inView:self.view]; 
} 

乾杯,

Tommy

+0

的文檔到目前爲止,這是正確的解決方案,沒有複雜性或創造非必要的抽象!,做得好湯米! – Wilson 2016-04-13 00:14:53

0

在我提到的線程鏈接的解決方案中,我創建了一個通用的拖放管理器,負責將拖動對象返回到原始位置(如果在已知的拖放區外發布)。目前解決方案中沒有動畫,但可以結合上面介紹的一些想法。

看到一些generic drag and drop solution in iOS

1

如何定義CGPoint在全球範圍內,還是在.h文件中。

CGPoint point;   

     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [button addTarget:self action:@selector(imageTouch:withEvent:)forControlEvents:UIControlEventTouchDown]; 
     [button addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside]; 
     [button setImage:[UIImage imageNamed:@"vehicle.png"] forState:UIControlStateNormal]; 
     [self.view addSubview:button]; 

後來我定義:

- (IBAction) imageMoved:(UIButton*) sender withEvent:(UIEvent *) event 
{ 
point = [[[event allTouches] anyObject] locationInView:self.view]; 
UIControl *control = sender; 
control.center = point; 
[sender addTarget:self action:@selector(touches:withEvent:) forControlEvents:UIControlEventTouchUpInside]; 
} 

-(void)touches:(UIButton *)button withEvent:(UIEvent *)event { 

[UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction animations:^{ 
    button.center = point; 

} completion:^(BOOL finished){ 
    if (finished) { 
    // do anything yu want 
    } 

它工作真棒,因爲我在我的項目中使用它!

相關問題