Xcode UIImageView动画跳回到原始位置



我对xcode很陌生。 想玩一玩,却遇到了这种奇怪的行为......

我正在使用 CoreMotion 加速在屏幕上移动图像。

缩短移动UIImageView移动代码...

        dispatch_async(dispatch_get_main_queue(), ^{
        CGRect recta = movingImage.frame;
        //data is the coremotion info.
        float dataAccelerationX = data.acceleration.x;
        float dataAccelerationY = -data.acceleration.y;
        float movetoX = recta.origin.x + (dataAccelerationX * stepMoveFactor);
        float maxX = self.view.frame.size.width - recta.size.width;
        float movetoY = (recta.origin.y + recta.size.height) - (dataAccelerationY * stepMoveFactor);
        float maxY = self.view.frame.size.height;
        movingImage.frame = recta;
    });

发生这种情况时,我有一个按钮,它只是在现有的 UIView 上创建另一个 UIImageView......

-(void)addInAnotherImage
{
    dispatch_async(dispatch_get_main_queue(), ^{
    NSLog(@"making UIImageView with image file");
    UIImageView *someImage =[[UIImageView alloc] initWithFrame:CGRectMake(200,200,200,200)];
    someImage.image=[UIImage imageNamed:@"someimage.png"];
    [self.view addSubview:someImage];
    });
}

发生这种情况时,移动图像会跳回到它开始的原始位置。 我希望使用 CoreMotion 的加速实现 addInAnotehrImage 在屏幕上浮动时触发 addInAnotehrImage。 任何人都可以帮助我确定为什么在运行 addInOtherImage 时移动图像会跳回到原始位置?

关系! 我发现了问题。

移动图像是用IBOutlet设置的。 对它有限制。

我所做的是将UIImageView类型从

IBOutlet UIImageView *movingImage

@property (nonatomic, strong) UIImageView *movingImage;

然后合成它,并在ViewDidLoad上设置开始坐标。

最新更新