0

我在这里几乎是个菜鸟,甚至写标题也很困难……但是这里有

我试图让我的应用程序执行此操作 1)用户按下按钮 2)按钮将按下转换为 int 值 3)然后将该值传递给 NStimer 并且计时器计数直到达到 int 值(即 30 秒如果 int 值为 30 4) 程序改变屏幕上的图像

我已经让它启动 NStimer 并使用变量按钮更改屏幕,通过手动更改此值,我可以将计时器设置为在不同时间后关闭

我不能做的是在按下按钮更改值后让计时器触发。如果我使用 NSLog,我可以看到值正在变化,但不会影响 NSTimer....任何想法

对不起,如果那有点啰嗦,但这里是代码......任何帮助都会很棒......因为我失去了大部分头发试图用 do while 循环和 if 语句解决这个问题

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    UIImageView *theImage;
    int imageCount;
    int button;
    button = 10;
}

@property (strong, nonatomic) IBOutlet UIImageView *theImage;
@property (strong, nonatomic) IBOutlet UIButton *press;


@end

//
//  ViewController.m
//  use ns timer to display an image
//
//  Created by Clive Dancey on 19/02/2013.
//  Copyright (c) 2013 Clive Dancey. All rights reserved.
//

#import "ViewController.h"



@interface ViewController ()

@property (strong, nonatomic) IBOutlet UILabel *label1;


@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [NSTimer scheduledTimerWithTimeInterval:button
                                     target:self
                                     selector:@selector(animateFunction)
                                     userInfo:nil
                                     repeats:YES];
}

- (IBAction)press:(id)sender
{
    button = 1;
}

- (void)animateFunction
{
    NSLog(@"Timer heartbeat %i", button);
    NSString *imageName = [NSString stringWithFormat:@"EZDecline.png"];
    [_theImage setImage:[UIImage imageNamed:imageName]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
4

1 回答 1

0

好的,我知道了,

1)从 viewDidLoad 中删除它

[NSTimer scheduledTimerWithTimeInterval:button
                             target:self
                             selector:@selector(animateFunction)
                             userInfo:nil
                             repeats:YES];

2) 将此行添加到 .h 文件

@property (nonatomic, strong) NSTimer *myTimer;

3)更改按钮按下事件中的代码,(像这样)

- (IBAction)press:(id)sender {
button=1;
if(_myTimer) [_myTimer invalidate];
_myTimer = [NSTimer scheduledTimerWithTimeInterval:button
                             target:self
                             selector:@selector(animateFunction)
                             userInfo:nil
                             repeats:YES];

}

于 2013-02-19T17:35:26.703 回答