0

背景:我正在使用 xcode 3.1.4 和 iphone 模拟器 3.1(我知道它已经过时,请不要对此发表评论)。

目标:我试图获得一个在视图加载后创建的 UIImageView 以不断向下移动。创建 UIImageView 工作正常,但移动功能不起作用。什么都没发生。我使用 NSTimer。这是我在 view controller.m 文件中的代码:

-(void)viewDidLoad{
    UIImageView *six = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Meteor.png"]];
    CGRect rectSix = CGRectMake(arc4random() % (250), arc4random() % (1), 35, 35);
    [six setFrame:rectSix];
    [self.view addSubview:six];
    [self moveMeteor:six];
    [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(moveMeteor:) userInfo:six repeats:YES];

}

-(void)moveMeteor:(UIImageView *)t{

    t.center=CGPointMake(t.center.x, t.center.y + 1); 


}

当然,我在 view controller.h 文件中声明了我的 moveMeteor 函数。通过输入:

-(void)moveMeteor:(UIImageView *)t;

关于问题是什么以及解决方案的任何想法?

4

2 回答 2

0

或者你可以做这样的事情,它利用 UIView 的 animateWithDuration 并且应该看起来更好。不需要计时器。

// in your interface declaration...
@property (nonatomic, assign) BOOL meteorShouldAnimate;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

// in your implementation...
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.meteorShouldAnimate = YES;
    [self moveMeteorWithAnimationOptions:UIViewAnimationOptionCurveEaseIn]; // ease in to the animation initially
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.meteorShouldAnimate = NO;
}


- (void)moveMeteorWithAnimationOptions: (UIViewAnimationOptions) options {
    __weak MyViewController * weakSelf = self;
    if (self.meteorShouldAnimate) {
        [UIView animateWithDuration:0.2 delay:0.0 options:options animations:^{
            self.imageView.transform = CGAffineTransformMakeTranslation(20.0, 20.0); // or whatever x and y values you choose
        } completion:^(BOOL finished) {
            MyViewController * strongSelf = weakSelf; // in case our view controller is destroyed before our completion handler is called
            if (strongSelf) {
                UIViewAnimationOptions nextOptions = strongSelf.meteorShouldAnimate ? UIViewAnimationOptionCurveLinear : UIViewAnimationOptionCurveEaseOut; // linear if we're continuing to animate, ease out if we're stopping
                [strongSelf moveMeteorWithAnimationOptions:nextOptions];
            }
        }];
    }
}
于 2013-07-22T22:31:13.467 回答
0

你的方法实际上没有得到正确的参数,你得到NSTimer了一个参数。所以与其

-(void)moveMeteor:(UIImageView *)t

方法应该是这样的

-(void)moveMeteor:(NSTimer *)timer

而且您不能将NSTimer其视为UIImageView. :)

*编辑

我建议你做类似的事情

-(void)viewDidLoad{
    UIImageView *six = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Meteor.png"]];
    CGRect rectSix = CGRectMake(arc4random() % (250), arc4random() % (250), 35, 35);
    [six setFrame:rectSix];
    [self.view addSubview:six];
    [self moveMeteor:six];
    [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(moveMeteor:) userInfo:six repeats:YES];    
}

并将您的方法更新为此

- (void)moveMeteor:(NSTimer *)timer {
    UIImageView *six = timer.userInfo;
    six.center=CGPointMake(six.center.x, six.center.y + 1); 
}

只是为了提供信息,我将UIImageView字典中的作为 userInfo 传递。

于 2013-07-22T21:32:53.300 回答