Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 转换UIView并设置标签文本_Ios_Objective C_Uiview_Uilabel_Uipangesturerecognizer - Fatal编程技术网

Ios 转换UIView并设置标签文本

Ios 转换UIView并设置标签文本,ios,objective-c,uiview,uilabel,uipangesturerecognizer,Ios,Objective C,Uiview,Uilabel,Uipangesturerecognizer,通过本教程,我开始学习iOS手势识别器。将ui标签”文本设置为手势速度时,使用ui感知识别器翻译视图时出现问题 我已经成功地用这样的平移手势翻译了一个视图(在IB的帮助下): PanViewController.h @interface PanViewController : UIViewController @property (weak, nonatomic) IBOutlet UIView *testView; @property (weak, nonatomic) IBOutlet

通过本教程,我开始学习iOS手势识别器。将
ui标签
”文本设置为手势速度时,使用
ui感知识别器翻译视图时出现问题

我已经成功地用这样的平移手势翻译了一个视图(在IB的帮助下):

PanViewController.h

@interface PanViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIView *testView;

@property (weak, nonatomic) IBOutlet UILabel *horizontalVelocityLabel;

@property (weak, nonatomic) IBOutlet UILabel *verticalVelocityLabel;

@end
PanViewController.m

@interface PanViewController ()

- (void)moveViewWithGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer;

@end

@implementation PanViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveViewWithGestureRecognizer:)];
    [self.testView addGestureRecognizer:panGestureRecognizer];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)moveViewWithGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer {
    CGPoint touchLocation = [panGestureRecognizer locationInView:self.view];

    self.testView.center = touchLocation;
}
但当我向该方法添加以下行时,视图停止平移,但UILabel会随着速度而更新:

CGPoint velocity = [panGestureRecognizer velocityInView:self.view];

self.horizontalVelocityLabel.text = [NSString stringWithFormat:@"Horizontal Velocity: %.2f points/sec", velocity.x];
self.verticalVelocityLabel.text = [NSString stringWithFormat:@"Vertical Velocity: %.2f points/sec", velocity.y];
我想知道我做错了什么。我怎样才能修好它?
提前感谢。

您能将整个识别器方法显示为不起作用的地方吗?以下是pan view controller-()的全部代码。识别器方法是
MoveViewWithGestureRecognitor:
。我尝试将对象移动到屏幕底部(或任何位置),但对象不移动。当我停止移动鼠标光标时,对象只是跳到光标所在的点。然后,当我再次移动它时,它首先会在移动之前将自身重置为中心。添加识别器方法的最后3行后出现此问题。我测试了您的代码(手动添加视图和标签),所有操作都正常。问题不在识别器代码中。自动布局打开了吗?@Dave,是的,自动布局打开了。在Main.storyboard中关闭自动布局解决了问题。太棒了!现在只需写一个问题的答案,详细说明解决问题的步骤,这样其他人就会受益。你不能将评论标记为正确答案,但你可以投票表决。