Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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 为什么不能同时在标签中移动图像和设置文本_Ios_Objective C_Cgpoint - Fatal编程技术网

Ios 为什么不能同时在标签中移动图像和设置文本

Ios 为什么不能同时在标签中移动图像和设置文本,ios,objective-c,cgpoint,Ios,Objective C,Cgpoint,我想将背景移到左边,就像我的角色正在行走一样,并显示行走的次数,但它不能同时这样做。此方法leftfootButton只能设置标签中的文本 - (IBAction)leftfootButton:(id)sender { _bgGround.center = CGPointMake(_bgGround.center.x -50, _bgGround.center.y); i = i+1; self.show.text = [NSString stringWithFormat

我想将背景移到左边,就像我的角色正在行走一样,并显示行走的次数,但它不能同时这样做。此方法leftfootButton只能设置标签中的文本

- (IBAction)leftfootButton:(id)sender {
    _bgGround.center = CGPointMake(_bgGround.center.x -50, _bgGround.center.y);
    i = i+1;
    self.show.text = [NSString stringWithFormat:@"%d",i];
}
如果我对设置文本的代码进行注释,那么它可以移动背景

- (IBAction)rightfootButton:(id)sender {
    _bgGround.center = CGPointMake(_bgGround.center.x - 50, _bgGround.center.y);
    i = i+1;
    //self.show.text = [NSString stringWithFormat:@"%d",i];
}

我该怎么办?

在使用“自动布局”时,不应直接更改帧,因为该方法会将帧返回到以前的位置。在许多情况下,系统会调用此方法。可以替代它以直接设置子视图的框架矩形。但我建议您更改背景视图的约束。即使您没有为视图设置约束,它们也会自动添加。

当您使用布局约束时,您永远不应该更改框架,您应该尝试更改约束,例如

为下面的视图创建一个IBOutlet for x Controint,确保其连接正确

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *xContraint;
然后在你的行动中

- (IBAction)leftfootButton:(id)sender {
  self.xContraint.constant -= 50;
  i = i + 1;
  self.show.text = [NSString stringWithFormat:@"%d", i];
}

是否使用自动布局?是的,我使用自动布局使用自动布局时不应直接更改帧,因为layoutSubviews方法会将帧返回到其以前的位置。此外,您正在将恒定帧设置为您的背景。我在没有自动布局的情况下创建新项目,但它仍然不工作尝试以下操作:启用自动布局,将前导约束添加到视图中,为该约束设置一个出口,并减小其常量值以向左移动视图。