首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在触摸时将标签向右滑动?

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

Stack Overflow用户
提问于 2013-01-21 15:44:01
回答 3查看 864关注 0票数 1

在我的应用程序中,我创建了一个UiLabel。我必须根据触摸手势向右移动标签,就像滑出菜单一样。请帮帮我。

EN

回答 3

Stack Overflow用户

发布于 2013-01-21 15:49:51

您可以使用UITapGestureRecognizer:

代码语言:javascript
运行
复制
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

[label addGestureRecognizer:singleTap];

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

代码语言:javascript
运行
复制
- (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
EN

Stack Overflow用户

发布于 2013-01-21 15:51:06

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

代码语言:javascript
运行
复制
// 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;
 }
}
票数 0
EN

Stack Overflow用户

发布于 2013-01-21 15:52:59

使用CGPointMake函数提供位置

代码语言:javascript
运行
复制
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"];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14434301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档