2014-05-09 79 views
0

我有UILabels獲得在屏幕上左右移動的動畫,並且我希望能夠在動畫時拖動它們。任何人有任何想法如何做到這一點?iOS - UILabels和手勢

WordLabel *firstWordLabel = [[WordLabel alloc] initWithFrame:CGRectMake(x, 100, 10, 35) andWithWord:firstWord]; 

firstWordLabel.text = firstWord.word; 
[firstWordLabel sizeToFit]; 
firstWordLabel.userInteractionEnabled = YES; 

UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] 
            initWithTarget:self 
            action:@selector(labelDragged:)]; 
[firstWordLabel addGestureRecognizer:gesture]; 

[self.view addSubview:firstWordLabel]; 

CGRect screenRect = [[UIScreen mainScreen] bounds]; 
CGFloat screenWidth = screenRect.size.width; 

CGRect newRect = firstWordLabel.frame; 
newRect.origin.x = floor(screenWidth/2); 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:2.0]; 
firstWordLabel.frame = newRect; 
[UIView commitAnimations]; 
+0

你應該從Facebook看看POP(https://github.com/facebook/pop)。據我所知,這是造成這個框架的主要原因。 – dasdom

+0

它是否允許您向其添加手勢? – Mikerizzo

+0

我不知道。但它是開源的。所以你可以看看你自己。 – dasdom

回答

0

不要你知道使用beginAnimationscommitAnimations是iOS 4的後勸阻,後來見this所以使用基於塊的動畫方法,而不是例如

ü可以使用如下 試試這個希望這個威力幫助

//first create a view which holds the label for example 
    UIView *holderView = [[UIView alloc]initWithFrame:CGRectMake(x, 100, 10, 35)]; 



    WordLabel *firstWordLabel = [[WordLabel alloc] initWithFrame:holderView.bounds andWithWord:firstWord]; 

    firstWordLabel.text = firstWord.word; 
    [firstWordLabel sizeToFit]; 
    firstWordLabel.userInteractionEnabled = YES; 

    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] 
           initWithTarget:self 
           action:@selector(labelDragged:)]; 
    [holderView addGestureRecognizer:gesture]; 

    [holderView addSubview:firstWordLabel]; 
    [self.view addSubview:holderView]; 

    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    CGFloat screenWidth = screenRect.size.width; 

    CGRect newRect = firstWordLabel.frame; 
    newRect.origin.x = floor(screenWidth/2); 

    //add the block based animation with option UIViewAnimationOptionAllowUserInteraction 
    [UIView animateWithDuration:2.0f delay:0.0f options:UIViewAnimationOptionAllowUserInteraction animations:^{ 
    holderView.frame = newRect; 

    } completion:^(BOOL finished) { 
     NSLog(@"completed"); 
    }]; 

什麼我不喜歡下面嘗試在新項目的測試

- (void)viewDidLoad 
    { 
     UIView *holderView = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 50, 35)]; 

     firstWordLabel = [[UILabel alloc]initWithFrame:holderView.bounds]; //is a member variable 

     firstWordLabel.backgroundColor = [UIColor greenColor]; 
     firstWordLabel.text = @"Hello"; 

     UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] 
            initWithTarget:self 
            action:@selector(labelDragged:)]; 
     [holderView addGestureRecognizer:gesture]; 
     firstWordLabel.userInteractionEnabled = YES; 
     [holderView addSubview:firstWordLabel]; 
     [self.view addSubview:holderView]; 

    } 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    [self startTheAnimation]; //calling animation 
    } 

    - (void)startTheAnimation 
{ 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    CGFloat screenWidth = screenRect.size.width; 

    CGRect newRect = firstWordLabel.frame; 
    newRect.origin.x = floor(screenWidth/2); 

    [UIView animateWithDuration:20.0f delay:0.0f options:UIViewAnimationOptionAllowUserInteraction animations:^{ 
    firstWordLabel.frame = newRect; 


    } completion:^(BOOL finished) { 
     NSLog(@"completed"); 
    }]; 

    } 

    - (void)labelDragged:(id)sender 
    { 
    NSLog(@"Dragged"); 
    } 
+0

仍然沒有抓住標籤上的手勢。不知道爲什麼不。 – Mikerizzo

+0

我會更新代碼只需2分鐘 –

+0

@Mikerizzo檢查現在我更新了代碼 –