iOS - UILabels和手势

时间:2014-05-09 11:46:08

标签: ios uilabel uigesturerecognizer

我有UILabel动画,它在屏幕上左右移动,我希望能够在动画时拖动它们。任何人都知道如何做到这一点?

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];

1 个答案:

答案 0 :(得分:0)

不知道在iOS 4之后使用beginAnimationscommitAnimations,之后会看到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");
   }