iPhone应用程序:每秒创建一个对象
我正在创建一个应用程序,它基本上是教程的扩展(我对应用程序开发世界非常陌生)。该教程有一个青色框在屏幕上移动到您触摸的位置。
我所做的是将背景更改为 ImageView,使其看起来像深海,然后将 ImageView 放入青色 UIView 中以显示潜水员的白色轮廓。
我想要从这个实验/练习中得到的是触发气泡每隔几秒从潜水员和屏幕底部的海藻中出现和消失。
我使用此 Quickie 链接 设置了计时器,但我收到了各种错误NSTimer 声明。
这是我的代码:
NSTimer *bubbleTimer;
bubbleTimer = [NSTimer scheduledTimerWithTimeInterval: 0.5
target: bubble
selector: @selector(moveBubble:)
userInfo: nil
repeats: YES];
NSTimer *bubbleTimer; 行不会为 NSTimer 着色,并且错误表明它默认 bubbleTimer 为 int。
正如我所说,我仍在努力在这个世界上找到自己的立足点,因此您可以提供的任何帮助将不胜感激
编辑:
这是我的 .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
和我的 .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{
//Begin 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
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{
//Begin 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能是你的格式,但是你的 NSTimer 是在方法中创建的吗?它似乎是自己飘出来的。
Might be your formatting, but is ur NSTimer created in a method? It seems to be floating out by itself.
听起来您缺少导入。
#import
Sounds like you're missing an import.
#import <Foundation/NSTimer.h>
您是否在文件顶部至少包含以下标头:
并与 Foundation 框架链接?
相关:请参阅另一篇 Stack Overflow 帖子 的有用答案
Are you including at least the following header at the top of your file:
and linking with the Foundation framework?
Related: see helpful answer from another Stack Overflow post
您发布的代码将不起作用,因为您正在实例方法之外执行实例变量 bubbleTimer 的初始化。当您希望计时器启动时,只需调用 scheduledTimer: 方法,但要从另一个实例方法内部调用(例如 TouchBegan:也许?但要小心,否则您将继续生成计时器...)而不是在打开时就像静态变量一样初始化。
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.