Objective c 在iOS 8中设置中心后视图跳回

Objective c 在iOS 8中设置中心后视图跳回,objective-c,timer,ios8,counter,Objective C,Timer,Ios8,Counter,我是Xcode的初学者,我的应用程序中的计数器有问题 当我用定时器数点时,playerpaddle在它所在的位置跳跃 第一次在中间 每次计时器计数点时 那我该怎么做才能让桨保持在原来的位置呢? 有没有其他方法来计算分数 无论如何使用滑动或g传感器控制播放器, 但在这个例子中,我用滑动控制它。 这个问题只出现在ios 8.0>中,而不出现在ios 7中 下面是一些代码: .h文件: .m文件: 在iOS 8中,自动布局是最重要的,设置中心不会更改布局约束。这与计时器无关。它与更改标签的文本有关。发

我是Xcode的初学者,我的应用程序中的计数器有问题

当我用定时器数点时,playerpaddle在它所在的位置跳跃 第一次在中间 每次计时器计数点时

那我该怎么做才能让桨保持在原来的位置呢? 有没有其他方法来计算分数

无论如何使用滑动或g传感器控制播放器, 但在这个例子中,我用滑动控制它。 这个问题只出现在ios 8.0>中,而不出现在ios 7中

下面是一些代码:

.h文件:

.m文件:


在iOS 8中,自动布局是最重要的,设置中心不会更改布局约束。这与计时器无关。它与更改标签的文本有关。发生这种情况时,它会触发整个视图的布局,并通过layoutIfNeeded重新确定约束。即使您可能没有在IB中设置任何约束,Xcode也会在构建过程中自动插入它们

有几种解决方案。首先,可以通过编程方式在viewDidLoad中插入拨片,这将绕过约束系统。对于这个特定的问题,这可能没关系,甚至可能是我个人的做法。大概是这样的:

@interface ViewController ()
@property (nonatomic, strong) UIView *paddle;
@end

@implementation ViewController

- (void)viewDidLoad {
    self.paddle = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
    self.paddle.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.paddle];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    self.paddle.center = CGPointMake(touchLocation.x, self.paddle.center.y);
}    
@end
但我们如何在不抛弃约束的情况下解决这个问题呢?我们可以修改约束条件。在IB中添加挡板视图,并为左侧、底部、宽度和高度添加约束。然后为水平和宽度约束创建IB插座

编辑水平空间约束以从第一个和第二个项目中删除相对边距:

现在,可以修改约束而不是中心:

@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIView *paddle;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *paddleHorizontalConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *paddleWidthConstraint;
@end

@implementation ViewController

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    CGFloat width = self.paddleWidthConstraint.constant;

    self.paddleHorizontalConstraint.constant = touchLocation.x - width/2;
}    
@end

您需要读取宽度约束,因为在调用viewDidLayoutSubviews:之前,帧或边界可能不正确。但您不想在此处更新约束,因为这将强制进行另一种布局。

虽然此代码存在一些风格问题,例如全局变量分数、变量名称中使用铅帽、使用IVAR作为分数标签和计时器,而不是属性,并且在解除时无法使计时器无效,因此如果解除视图控制器并重新加载它,您将获得双倍计分,我没有看到任何会导致您描述的行为的错误。我怀疑这个错误在你的代码中的其他地方。谢谢你的快速回答!我已经做了测试应用程序只是为了这个问题,我没有在它的其他代码。不过还是发生了……非常感谢!这让我很开心!
@interface ViewController ()
@property (nonatomic, strong) UIView *paddle;
@end

@implementation ViewController

- (void)viewDidLoad {
    self.paddle = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
    self.paddle.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.paddle];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    self.paddle.center = CGPointMake(touchLocation.x, self.paddle.center.y);
}    
@end
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIView *paddle;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *paddleHorizontalConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *paddleWidthConstraint;
@end

@implementation ViewController

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    CGFloat width = self.paddleWidthConstraint.constant;

    self.paddleHorizontalConstraint.constant = touchLocation.x - width/2;
}    
@end