【问题标题】:iOS: Regenerating UIImageViewiOS:重新生成 UIImageView
【发布时间】:2014-10-19 03:38:22
【问题描述】:

我正在制作一个非常简单的应用程序。我有一个 UIButton 和一个 UIImageView,每次按下按钮时,图像都会移动 x:-44 和 y:-41。我想要做的是,当图像到达屏幕中的某个(x,y)点时,在屏幕的其他点重新生成。我希望这是有道理的。

这是我的代码:

文件。 h

{

IBOutlet UIImageView *ImageOne;

}

-(IBAction)Button:(id)sender;

文件。米

-(IBAction)Button:(id)sender{


//ImageOne moving down the screen

[UIView animateWithDuration:0.1f
                 animations:^{

                     ImageOne.center = CGPointMake(ImageOne.center.x -44, ImageOne.center.y +41);

                 }];

如何使用 if 语句做到这一点?

提前感谢您!

【问题讨论】:

    标签: ios objective-c xcode uiimageview uibutton


    【解决方案1】:

    这取决于。如果您想在移动完成之前简单地监控位置,那么您可以执行以下操作:

    -(IBAction)Button:(id)sender{
    CGPoint newCenter = ImageOne.center;
    
    // check the image's position
    if (ImageOne.center.x < some_x) {
        newCenter.x = some_other_x;
    } else if (ImageOne.center.y > some_y) {
        newCenter.y = some_other_y;
    } else {
        newCenter =  CGPointMake(ImageOne.center.x -44, ImageOne.center.y +41);
        //ImageOne moving down the screen
    
        [UIView animateWithDuration:0.1f
                         animations:^{
                             ImageOne.center = newCenter;
                         }];
    }
    

    或者如果您想在移动过程中捕捉到它并修改位置,您应该尝试Key-Value Observing 以查看.center 何时更改。

    我不确定这个常规的UIView 的动画是否适用于KVO,也许您需要使用Facebook's Pop animation library 来实现动画。

    【讨论】:

      猜你喜欢
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      • 2012-12-13
      • 2011-07-01
      • 1970-01-01
      • 2014-07-02
      相关资源
      最近更新 更多