触摸时如何将标签向右滑动?

时间:2013-01-21 07:44:01

标签: ios ios5 ios6 slider uilabel

在我的应用程序中,我创建了一个UiLabel。我必须根据触摸手势将标签移动到右侧,例如“滑出”菜单。请帮帮我。

3 个答案:

答案 0 :(得分:1)

您可以使用UITapGestureRecognizer:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

[label addGestureRecognizer:singleTap];

然后在你的方法中使用UIViewAnimation:

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {


     CGRect frame = label.frame;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

     frame.origin.x += 100; // slide right
    label.frame = frame;

    [UIView commitAnimations];
}

答案 1 :(得分:0)

假设UILabel插座名称正在测试中。

// set up geture as follows in view did load
UISwipeGestureRecognizer* swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(labelDidSwiped:)];
[swipeGesture setDirection: UISwipeGestureRecognizerDirectionLeft];
[testing addGestureRecognizer:swipeGesture];

swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(labelDidSwiped:)];
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionRight];

[testing addGestureRecognizer:swipeGesture];

-(void)labelDidSwiped:(UISwipeGestureRecognizer*)gesture
{
switch (gesture.direction) {
    case UISwipeGestureRecognizerDirectionLeft:
        // you can change the frame of label in here depending on your requirement
        DLog(@"left swipe");
        break;
    case UISwipeGestureRecognizerDirectionRight:
// you can change the frame of label in here depending on your requirement
        break;
    default:
        break;
 }
}

答案 2 :(得分:0)

使用CGPointMake功能提供位置

label.position = CGPointMake (newX, newY);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.duration = <duration>;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
animation.fromValue = CGPointMake (oldX, oldY);
animation.toValue = CGPointMake (newX, newY);
[label addAnimation:animation forKey:@"position"];