2014-05-09 53 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检查现在我更新了代码 –