开发者

iPhone Application: Create an object every second

开发者 https://www.devze.com 2023-01-17 13:39 出处:网络
I\'m creating an app that is basically an extension of a tutorial (I am very new to the app development world). The tutorial had a cyan coloured box moving around the screen to the point where you tou

I'm creating an app that is basically an extension of a tutorial (I am very new to the app development world). The tutorial had a cyan coloured box moving around the screen to the point where you touch.

What I did was change the background to an ImageView so it looks like the deep sea and I put a ImageView inside the cyan UIView to display the white silhouette of a diver.

What I want from this experiment/exercise is to trigger bubbles to appear and disappear every couple of seconds both from the diver and the seaweed at the bottom of the screen.

I set up my timer using this Quickie link but I'm getting all sorts of errors from the NSTimer declaration.

Here is my code:

NSTimer *bubbleTimer;

bubbleTimer = [NSTimer scheduledTimerWithTimeInterval: 0.5
                                     target: bubble
                                   selector: @selector(moveBubble:)
                                   userInfo: nil
                                    repeats: YES];

The line NSTimer *bubbleTimer; does not colour the NSTimer, and an error said that it's defaulting bubbleTimer to an int.

As I've said, I'm still trying to find my feet in this world so any help you can give will be appreciated

EDIT:

here's my .h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface TouchView : UIView {

IBOutlet UIView *stretchView; //This defines an object in Interface Builder called stretchView
IBOutlet UIView *bubble;
NSTimer *bubbleTimer;
}

@end

and my .m

#import "TouchView.h"


@implementation TouchView

//First we need a method that processes each touch of the screen
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

//Beg开发者_运维问答in an animation - The animation is given a name which in this case is
//Move and Stretch
[UIView beginAnimations:@"MoveAndStretch" context:nil];
//We set a duration for this animation in seconds
[UIView setAnimationDuration:1];
//Start the animation from the screen's current state
[UIView setAnimationBeginsFromCurrentState:YES];
//Any location changes between here and Commit Animations is animated.
//Firstly we need to process a simple touch which 
if([touches count] == 1) {
    //make a single touch variable
    UITouch *touch = [touches anyObject];
    //change the location of stretchview to the location of the touch
    stretchView.center = [touch locationInView:self];
}
[UIView commitAnimations];
}

- (void)moveBubble:(NSTimer *)theTimer {
CGPoint start;
start.x=480;
start.y=300;
CGPoint end;
end.x=0;
end.y=300;
bubble.center=start;
[UIView beginAnimations:@"Bubble" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationBeginsFromCurrentState:YES];
bubble.center=end;
[UIView commitAnimations];
}

bubbleTimer = [NSTimer scheduledTimerWithTimeInterval: 0.5
                                                        target: self
                                                      selector: @selector(moveBubble:)
                                                          userInfo: nil
                                                           repeats: YES];

@end


Might be your formatting, but is ur NSTimer created in a method? It seems to be floating out by itself.


Sounds like you're missing an import.

#import <Foundation/NSTimer.h>


Are you including at least the following header at the top of your file:

#import <Foundation/Foundation.h>

and linking with the Foundation framework?

Related: see helpful answer from another Stack Overflow post


The code you posted will not work since you are performing an initialization of the instance variable bubbleTimer outside of an instance method. Just call the scheduledTimer: method when you want the timer to start, but from inside another instance method (like touchesBegan: maybe? But be careful or you'll continue to spawn timers...) and not in the open like a static variable initialization.

0

精彩评论

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

关注公众号