I want to repeat an image again and again.But when i run my code it crashes with a warning "MainViewController may not respond to setneedsDisplay" and also"uiviewController may not respond to initwithFrame". Here is my code.
#import "MainViewController.h"
#implementation MainViewController
#synthesize car,road;
UIImage *currentImage;
int tileOffset=0;
-(void)awakeFromNib{
[[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:NO];
[car setAlpha:0];
[road setAlpha:0];
currentImage=[UIImage imageNamed:#"Road3.png"];
[NSTimer scheduledTimerWithTimeInterval:(.1) target:self selector:#selector(onTimer)
userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:(.01) target:self selector:#selector(onTimerRoad) userInfo:nil repeats:YES];
}
-(void)onTimerRoad{
tileOffset+=1;
[self setNeedsDisplay]; //here Get the warning
}
-(void)onTimer{
[self update1];
}
-(void)update1{
[self updateRoad];
}
-(void)updateRoad{
[self randomRoadUpdate];
}
-(void)randomRoadUpdate{
CGPoint oldPosition=road.center;
road.center=CGPointMake(oldPosition.x, oldPosition.y+1);
}
-(id)initWithFrame:(CGRect)frame{
if(self=[super initWithFrame:frame]){//here get warning
}
return self;
}
-(void)drawRect:(CGRect)rect{
CGImageRef image=CGImageRetain(currentImage.CGImage);
CGRect imageRect;
imageRect.origin=CGPointMake(160, 240);
imageRect.size=CGSizeMake(320, 480);
CGContextRef uicontext=UIGraphicsGetCurrentContext();
CGContextClipToRect(uicontext, CGRectMake(0, 0, rect.size.width, rect.size.height));
CGContextDrawTiledImage(uicontext, imageRect, image);
}
Thanks in advance..
setNeedDisplays is a method for UIView.... So you need to call [self.view setNeedDisplay] the second warning is about your assignment statement, try this to get it out:
self = [super initWithFrame:frame];
if(self){
//...
}
Related
myTimer is defined in OneViewController and i m trying to invalidate and fire it again from mainviewcontroller but it is not working. What exactly i m missing here.
here is my code
OneViewController.h
#property (nonatomic, strong) NSTimer *myTimer;
OneViewController.m
#synthesize myTimer;
- (void)viewDidLoad
{
[super viewDidLoad];
[self myTimerMethod];}
- (void)myTimerMethod{
NSLog(#"myTimerMethod is Called");
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:2.4
target:self
selector:#selector(updateView:)
userInfo:nil
repeats:YES];
}
- (void)updateView:(NSTimer *)theTimer
{
if (index < [textArray count])
{
self.textView.text = [self.textArray objectAtIndex:index];
self.imageView.image = [self.imagesArray objectAtIndex:index];
index++;
}else{
index = 0;
}
}
MainViewController.h
#class OneViewController;
#property (strong, nonatomic) OneViewController *oneviewcontroller;
MainViewController.m
#synthesize oneviewcontroller = _oneviewcontroller;
-(void)playpauseAction:(id)sender {
if([audioPlayer isPlaying])
{
[sender setImage:[UIImage imageNamed:#"music.png"] forState:UIControlStateNormal];
[audioPlayer pause];
[self.oneviewcontroller.myTimer invalidate];
}else{
[sender setImage:[UIImage imageNamed:#"audiostop.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self.oneviewcontroller.myTimer fire];
if(isFirstTime == YES)
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:06.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
isFirstTime = NO; } } }
- (void)displayviewsAction:(id)sender{
OneViewController *oneviewcontroller = [[OneViewController alloc] init];
oneviewcontroller.view.frame = CGRectMake(0, 0, 320, 430);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionFade];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFade];
[self.view addSubview:oneviewcontroller.view];
}
Appreciate help.
Thanks.
You create OneViewController as a local variable of the method displayviewsAction. Not as the property you defined previously.
That's why it's not working, the variable gets released once you exit the method.
Change this line:
OneViewController *oneviewcontroller = [[OneViewController alloc] init];
With this one:
self.oneviewcontroller = [[OneViewController alloc] init];
I am getting warning message in red statement requires expression of scalar type ('void' invalid) at the if statement.
- (void)displayviewsAction:(id)sender
{
FirstViewController *viewController = [[FirstViewController alloc] init];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
if ([audioPlayer pause]){
[timer invalidate];
} else {
self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:#selector(secondViewController) userInfo:nil repeats:NO];
}
[viewController release];
}
Any idea how to fix it.
Thanks for help.
Looks like [audioPlayer pause] does not return any value.
Assuming that audioPlayer is an instance of MPMusicPlayerController, try
if(audioPlayer.playbackState == MPMusicPlaybackStatePaused)
Want to pause the multiple views from updating when Pause button is pressed
In h file
#property BOOL appIsPaused;
In m file
#synthesize appIsPaused;
-(void)playpauseAction:(id)sender
{
if
([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:#"play.png"] forState:UIControlStateSelected];
[audioPlayer pause];
appIsPaused = YES;
} else {
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
appIsPaused = NO;
[self performSelector:#selector(displayviewsAction:) withObject:nil afterDelay:11.0];
}
}
- (void)displayviewsAction:(id)sender
{
FirstViewController *viewController = [[FirstViewController alloc] init];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
[self performSelector:#selector(secondViewController) withObject:nil afterDelay:23];
[viewController release];
}
-(void)secondViewController {
SecondViewController *secondController = [[SecondViewController alloc] init];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:secondController.view];
[self.view addSubview:toolbar];
[self performSelector:#selector(ThirdviewController) withObject:nil afterDelay:27];
[secondController release];
}
and it goes on like this for multiple views.
Any ideas how to pause views from updating whenever pause button is pressed.
Instead of using performSelector after delay, you should consider using a NSTimer.
Like this:
Declare a NSTimer *timer ivar.
Declare a NSUInteger viewControl;
Set viewControl to 0;
On the play part of the method add this line:
timer = [NSTimer scheduledTimerWithTimeInterval:11 target:self selector:#selector(tick) userInfo:nil repeats:YES];
-(void)tick
{
switch(viewControl)
{
case 0:
[self performSelector:#selector(firstViewController) withObject:nil];
break;
case 1:
[self performSelector:#selector(secondViewController) withObject:nil];
break;
case 2:
[self performSelector:#selector(thirdViewController) withObject:nil];
break;
.
.
.
default:
break;
}
viewControl++;
if(viewControl > MAX_VIEWS)
{
viewControl = 0;
}
}
And add this line on pause action:
[timer invalidate]
It is also cleaner and let you have more control over your code.
Hope it helps.
Hi I am trying to splash screen with the help of timer. but it can't. IS any suggestion regarding this code.........
SplashViewController.h:-
#import <UIKit/UIKit.h>
#import "MainMenu.h"
#interface SplashViewController : UIViewController {
UIImage *imgSplash;
NSTimer *timer;
MainMenu *objMainMenuView;
}
#property (nonatomic,retain) NSTimer *timer;
#property (nonatomic,retain) UIImage *imgSplash;
#property (nonatomic,retain) MainMenu *objMainMenuView;
#end
SplashViewController.m:-
#import "SplashViewController.h"
#implementation SplashViewController
#synthesize timer,imgSplash,objMainMenuView;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
timer = [[NSTimer alloc] init];
UIImageView *splashImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,460)];
imgSplash = [[UIImage alloc] init];
imgSplash = [UIImage imageNamed:#"chicocredit_splash.png"];
[splashImageView setImage:imgSplash];
[self.view addSubview:splashImageView];
timer = [NSTimer timerWithTimeInterval:2.0 target:self.timer selector:#selector(fadeScreen) userInfo:nil repeats:NO];
if([timer isValid]==1)
{
[timer fire];
//self.view.alpha = 0.0;
objMainMenuView = [[MainMenu alloc] initWithNibName:#"MainMenu" bundle:nil];
[self.navigationController pushViewController:objMainMenuView animated:YES];
}
}
-(void) onTimer{
NSLog(#"LOAD");
}
- (void)fadeScreen
{
[UIImageView beginAnimations:nil context:nil];
[UIImageView setAnimationDuration:2.0];
[UIImageView setAnimationDelegate:self];
[UIImageView commitAnimations];
//[UIView setAnimationDidStopSelector:#selector(finishedFading)];
//self.view.alpha = 0.0;
//[UIView commitAnimations];
}
/*
- (void) finishedFading
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.0];
//self.view.alpha = 1.0;
//viewController.view.alpha = 1.0;
//self.objMainMenuView.view.alpha = 1.0;
[UIView commitAnimations];
//[splashImageView removeFromSuperview];
}*/
- (void)dealloc {
[super dealloc];
}
#end
Use this It may be solution for that.
- (void)viewDidLoad {
timer = [[NSTimer alloc] init];
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 460.0f);
splashImageView = [[UIImageView alloc] initWithFrame:myImageRect];
[splashImageView setImage:[UIImage imageNamed:#"image3.jpg"]];
splashImageView.opaque = YES;
[self.view addSubview:splashImageView];
[splashImageView release];
[self performSelector:#selector(doTHis) withObject:nil afterDelay:2.0];
[super viewDidLoad];
}
-(void)doTHis{
objMainMenuView = [[MainMenu alloc] initWithNibName:#"MainMenu" bundle:nil];
[self.navigationController pushViewController:objMainMenuView animated:YES];
}
Good Luck
Looks like your MianMenu is getting pushed before you can see the fade effect. Its because you are firing the timer and pushing the Main Menu immediately.
// Schedule the timer here.
[NSTimer timerWithTimeInterval:2.0f target:self selector:#selector(timerFireMethod:) userInfo:nil repeats:NO];
// Timer fire method.
-(void) timerFireMethod:(NSTimer *) theTimer {
[UIView beginAnimations:nil context: nil];
[UIView setAnimationDuration:2.0f];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector: #selector(animationDidStop: finished: context:)];
// Put animation code.
[UIView commitAnimations];
}
// Called when animation finishes.
-(void) animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
// Called when animation finishes.
// Push the Main menu here.
}
Keep break point in the timer fire method. It should get called after 2 seconds as specified.
Then keep break point in the animationDidStopSelector method. It will get called after your fade animation of 2 seconds.
Your code is leaked:
1:
timer = [[NSTimer alloc] init]; //Leak here, remove this line
.
.
.
timer = [NSTimer timerWithTimeInterval:2.0 target:self.timer selector:#selector(fadeScreen) userInfo:nil repeats:NO];
2:
imgSplash = [[UIImage alloc] init];//Leak here, remove this line
imgSplash = [UIImage imageNamed:#"chicocredit_splash.png"];
3:
UIImageView *splashImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,460)];
...
[splashImageView setImage:imgSplash]; //Leak here, should release splashImageView
Why not, do everything else after timer expired and fading is done.
- (void) finishedFading
{
objMainMenuView = [[MainMenu alloc] initWithNibName:#"MainMenu" bundle:nil];
[self.navigationController pushViewController:objMainMenuView animated:YES];
}
short cut: use another viewController with imageView present it modally and dismiss with animation dissolve.
You should present view in viewWillAppear.Start timer on viewLoad of second viewController.When timer fires dismiss it.
What I want to do is that every ten second a ball is created outside of the screen, and when it is created it move to the center of the screen. How can I do this?
In your view controller set up a UIImageView property for your ball, contruct the ball and make sure its starting location is off the viewable area of the creen and then use an NSTimer which fires every ten seconds which callas a method that animates your ball across the screen to the centre.
-(void)viewDidLoad{
NSTimer *ballTimer = [NSTimer timerWithTimeInterval:10.0
target:self
selector:#selector(moveBall)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:ballTimer forMode:NSRunLoopCommonModes];
myBall = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MyBallImageFile.png"]];
myBall.center = CGPointMake(320/2, [self randNumBetween:-50:-100]);
[self.view addSubview:myBall];
}
-(void)moveBall{
myBall.center = CGPointMake(320/2, [self randNumBetween:-50:-100]);
[UIView animateWithDuration:5.0 animations:^{
myBall.center = CGPointMake(320/2, 480/2);
}];
}
----------EDIT------------
- (CGFloat)randNumBetween:(CGFloat) min :(CGFloat) max{
CGFloat difference = max - min;
return (((CGFloat) rand()/(CGFloat)RAND_MAX) * difference) + min;
}
- (void)viewDidLoad {
[super viewDidLoad];
myBallImage = [[UIImage imageNamed:#"ball.png"] retain];
myTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(addBallToScreen) userInfo:nil repeats:YES];
}
- (void)viewDidUnload {
[myBallImage release];
[myTimer invalidate];
}
- (void)addBallToScreen {
//Create the imageView
UIImageView *imageView = [[UIImageView alloc] initWithImage:myBallImage];
imageView.transform = CGAffineTransformMakeTranslation(1000, 1000);
[self.view addSubview:imageView];
[imageView release];
//Animate the image view
[UIView beginAnimations:nil context:nil];
imageView.transform = CGAffineTransformMakeTranslation(50, 50);
[UIView commitAnimations];
}