开发者

Help with Xcode

开发者 https://www.devze.com 2023-01-17 12:11 出处:网络
I am totally new to Xcode, and I need a little bit of help... I\'ve created a cocoa project, and then opened the main.xib file in \"Interface Builder\".

I am totally new to Xcode, and I need a little bit of help...

I've created a cocoa project, and then opened the main.xib file in "Interface Builder".

From Interface Builder, I added an NSImageView object, and set the image to a gif file inside my resources folder in the project.

I need to know how exactly I can program the application so that the NSImageView c开发者_StackOverflow中文版hanges its location every millisecond or so.

Can anyone enlighten me?

--G


Once you've connected the imageView to an outlet in your .h file, implement the following:

In your .h file:

@interface YourViewController : UIViewController{
    NSTimer *mainTimer;
    BOOL timerIsEnabled;
}

in your .m file:

- (void)viewDidLoad
{
    mainTimer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
    timerIsEnabled = TRUE;
    [super viewDidLoad];
}

-(void)updateTime{
    if (timerIsEnabled) {

        float dx = arc4random()%768+1;
        float dy = arc4random()%1004+1;
        CGPoint newPoint = CGPointMake(dx, dy);


        UIView *touchView = imageView;
        float midPointX = CGRectGetMidX(touchView.bounds);
        // If too far right...
        if (newPoint.x > self.view.bounds.size.width  - midPointX)
            newPoint.x = self.view.bounds.size.width - midPointX;
        else if (newPoint.x < midPointX)  // If too far left...
            newPoint.x = midPointX;

        float midPointY = CGRectGetMidY(touchView.bounds);
        // If too far down...
        if (newPoint.y > self.view.bounds.size.height  - midPointY)
            newPoint.y = self.view.bounds.size.height - midPointY;
        else if (newPoint.y < midPointY)  // If too far up...
            newPoint.y = midPointY;

        touchView.center = newPoint;
    }
}

- (void)viewDidUnload
{
    [mainTimer invalidate];
    [self setImageView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

You can also create a button to start and stop the timer:

- (IBAction)go:(id)sender {
    timerIsEnabled = !timerIsEnabled;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号