make an object move across the screen iphone? - iphone

I want to simply have a loop so that an object continuously moves across the screen at the bottom. Here is my code it should be pretty easy to understand.
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:#selector(spawnRocket) withObject:self afterDelay:2]; //delay before the object moves
}
-(void)spawnRocket{
UIImageView *rocket=[[UIImageView alloc]initWithFrame:CGRectMake(-25, 528, 25, 40)]; //places imageview right off screen to the bottom left
rocket.backgroundColor=[UIColor grayColor];
[UIView animateWithDuration:5 animations:^(){rocket.frame=CGRectMake(345, 528, 25, 40);} completion:^(BOOL finished){if (finished)[self spawnRocket];}]; //this should hopefully make it so the object loops when it gets at the end of the screen
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
After doing all this i click run and all i see is a white screen on my iphone 6.0 simulator
ps. im running xcode 4.5.1

A few things:
UIImageView *rocket=[[UIImageView alloc]initWithFrame:...
You're not assigning an image to the image view, the best way to do this is to use:
UIImage* image = [UIImage imageNamed:#"image.png"];
UIImageView *rocket = [[UIImageView alloc] initWithImage:image];
rocket.frame = CGRectMake(-25, 528, 25, 40);
(The root cause of your problem) You are not adding your UIImageView to your main view, hence it's not being displayed. In spawnRocket, you should do:
[self.view addSubview:rocket];
Note: Because you want this to be done in a loop, you're gonna have to make sure your memory management is in order.
I don't know whether you still want the rocket on screen after it's finished moving, but if not, remember to keep a reference to the UIImageView and removeFromSuperview when you're done (to prevent memory leaks).
Calling spawnRocket in viewDidLoad is probably not the best idea, it may not reach the screen yet when spawnRocket is called. Try calling it in viewWillAppear or viewDidAppear (whatever is best in your case)
[self performSelector:#selector(spawnRocket) withObject:self afterDelay:2];
You don't need to provide self within withObject:, you're not accepting any parameters within spawnRocket

You don't add the UIImageView to any parent view. It will just live in memory, but not be displayed. Add it to your view controller's view after creating it:
[self.view addSubview:rocket];

Related

Custom UIView layoutSubviews with orientation and animations?

I'm in a dilemma which method to use for setting frames of custom UIViews with many subviews in it and still have animations and automatically adjust to rotations. What I usually do when I create a new viewcontroller is alloc my custom view in loadView or viewDidLoad, e.g:
-(void)viewDidLoad
{
[super viewDidLoad];
detailView = [[DetailView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
detailView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.view = detailView;
}
Normally this width & height is not correct for an iPhone5-screen (the actual view-frame is not set until viewWillAppear) but because of the autoresizingmask it all works out.
Then in the initWithFrame of the custom UIView DetailView, I alloc all subviews with CGRectZero, e.g:
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
label = [[UILabel alloc] initWithFrame:CGRectZero];
[self addSubview:label];
}
}
Then I override layoutsubviews to set all frames of all subviews. This works perfectly for any screen size and any orientation, e.g:
-(void)layoutSubviews
{
[super layoutSubviews];
label.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}
However, I just found out that layoutSubviews is not so great when you use animations, because when you use animations in an animationblock, layoutsubviews is called in the middle of the animation and it completely breaks the animation, e.g:
-(void)animateLabel
{
[UIView animateWithDuration:0.4f animations:^
{
label.transform = CGAffineTransformMakeTranslation(100, 100);
}];
}
I believe there are ugly workarounds this by using flags for each animation and in layoutsubviews use those flags to set the correct start or endframe of the animated block but I don't think I should have to create a flag for each animation I want to do.
So my problem is now: how am I supposed to have a custom UIView WITH animations that also automatically adjusts itself to rotations?
The only solution I can come up with right now (that I don't like):
Don't use layoutSubviews but use the setFrame/setBounds method of the custom UIView to set the frames of all subviews. Then check in the viewController every time a rotation occurs and then use the setFrame/setBounds method of the custom UIView to change all frames of all subviews. I don't like this solution because the rotation methods are different in iOS5 and iOS6 and I don't want to have to do this in every UIViewController with it's own custom UIView.
Any suggestions?
I have recently started overriding viewDidLayoutSubviews (many times instead of viewWillAppear) in my UIViewControllers.
Yes viewDidLayoutSubviews is called on rotations. (from comment)
The method fires after all the internal layouts have already been completed so all finalized frames should be setup, but still give you the time you need to make adjustments before the the view is visible and shouldn't have any issues with animations because you are not already inside an animation block.
viewcontroller.m
- (void)viewDidLayoutSubViews {
// At this point I know if an animation is appropriate or not.
if (self.shouldRunAnimation)
[self.fuView runPrettyAnimations];
}
fuView.m
- (void)runPrettyAnimations {
// My animation blocks for whatever layout I'd like.
}
- (void)layoutSubviews {
// My animations are not here, but non animated layout changes are.
// - Here we have no idea if our view is visible to the user or may appear/disappear
// partway through an animation.
// - This also might get called far more than we intend since it gets called on
// any frame updates.
}

UIScrollView will not zoom

I have a subclassed UIView which I can get to scroll but not to zoom. I'm using autolayout so wondered if anything had changed in IOS6. In particular when are the scrollViewWillBeginZooming and scrollViewDidEndZooming methods implemented. My code looks like
- (void)viewDidLoad
{
[super viewDidLoad];
self.ringSet2 = [[RingView alloc] initWithFrame:CGRectMake(0, 0, 800, 800)];
[self.ringSet2 setDefaults];
/// ... more setup for other views but only ringSet2 is scrolled.
self.scrollview1.delegate=self;
self.scrollview1.scrollEnabled=YES;
self.scrollview1.contentSize=self.ringSet2.bounds.size
self.scrollview1.minimumZoomScale=0.2;
self.scrollview1.maximumZoomScale=5.0;
self.ringSet2.userInteractionEnabled=YES;
// ... needed elsewhere so other views can pick up their dimensionts
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
/// ... code for additional views
[self.scrollview1 zoomToRect:CGRectMake(0, 0, 200, 200) animated:YES];
[self.scrollview1 addSubview:self.ringSet2];
}
with
-(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.ringSet2;
}
and scrollViewWillBeginZooming etc. implemented just to trace what is happening. Interesting,
viewForZoomingInScrollView appears to get called only once, as is scrollViewDidEndZooming with a scale value just under one but scrollViewWillBeginZooming is never called. The property ringSet2 is defined
#property (strong, nonatomic) IBOutlet RingView *ringSet2;
as the view does not appear if it's defined as weak.
Apologises for this. The answer is really stupid. I'm currently developing under IOS 6.0 on the simulator whilst my phone is still on IOS 5. The simulator allows you to simulate pinches but only centred on the centre of the screen. The scrollview however only receives this signal if it too is in the centre which was not the case in my initial build. Moving the scroll view's rectangle into the centre fixed the problem. You would not make this mistake testing on a real device. The code is thus OK. Lost 3 or 4 evenings pulling my hair over this one.

Animation stops when using UINavigationController or UITabView

I have a strange bug that I can't seem to figure out. I'm creating a little shining animation, which works perfectly, but for some reason stops when I navigate to another view via UINavigationController or UITabView (strangely modal view's don't affect it). Any ideas why, and how I can make sure the animation doesn't stop?
UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[whiteView setBackgroundColor:[UIColor whiteColor]];
[whiteView setUserInteractionEnabled:NO];
[self.view addSubview:whiteView];
CALayer *maskLayer = [CALayer layer];
maskLayer.backgroundColor = [[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.0f] CGColor];
maskLayer.contents = (id)[[UIImage imageNamed:#"ShineMask.png"] CGImage];
// Center the mask image on twice the width of the text layer, so it starts to the left
// of the text layer and moves to its right when we translate it by width.
maskLayer.contentsGravity = kCAGravityCenter;
maskLayer.frame = CGRectMake(-whiteView.frame.size.width,
0.0f,
whiteView.frame.size.width * 2,
whiteView.frame.size.height);
// Animate the mask layer's horizontal position
CABasicAnimation *maskAnim = [CABasicAnimation animationWithKeyPath:#"position.x"];
maskAnim.byValue = [NSNumber numberWithFloat:self.view.frame.size.width * 9];
maskAnim.repeatCount = HUGE_VALF;
maskAnim.duration = 3.0f;
[maskLayer addAnimation:maskAnim forKey:#"shineAnim"];
whiteView.layer.mask = maskLayer;
Your maskAnim is retained by maskLayer, which is retained by whiteView's layer, which is retained by whiteView, which is retained by self.view. So this entire object graph will live until your view controller's view gets dealloc'd.
When you navigate away from your view controller, UIKit unloads your view to free up memory. When the view gets dealloc'd, so does your maskAnim. When you navigate back to your view controller, UIKit reconstructs your view hierarchy, either by reloading it from its .xib, or by calling loadView, depending on which technique you used.
So you need to make sure that the code you used to set up your maskAnim gets called again after UIKit reconstructs your view hierarchy. There are four methods you might consider: loadView, viewDidLoad, viewWillAppear:, and viewDidAppear:.
loadView is a sensible option if you use that method to build your view hierarchy (as opposed to loading it from a .xib), but you'll have to change your code so that it doesn't depend on the self.view property, which would trigger loadView recursively. viewDidLoad is also a good choice. With either of these options you you need to be careful because UIKit might resize your view after viewDidLoad if it wasn't constructed at the right size. This could cause bugs since your code depends on self.view.frame.size.width.
If you set up your animation in viewWillAppear: or viewDidAppear:, you can be sure that your view's frame will be the proper dimension, but you need to be careful with these methods because they can get called more than once after the view gets loaded, and you don't want to add your whiteView subview more than once.
What I'd probably do is make whiteView a retained property, and lazy load it in viewWillAppear: like this:
- (UIView *)setupWhiteViewAnimation {
// Execute your code above to setup the whiteView without adding it
return whiteView;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (!self.whiteView) {
self.whiteView = [self setupWhiteViewAnimation];
[self.view addSubview:self.whiteView];
}
}
- (void)viewDidUnload {
self.whiteView = nil; // Releases your whiteView when the view is unloaded
[super viewDidUnload];
}
- (void)dealloc {
self.whiteView = nil; // Releases your whiteView when the controller is dealloc'd
[super dealloc];
}

UIImageView showing the image only after changing tabs in the UITabBarViewController

I have a UIScrollViewController that has a UIScrollView which holds an UIImageView inside.
In loadView of the the scroll view controller I download the image by calling a function, that's supposed to do the downloading on a different thread. I call this function from my loadView and then put the imageview inside the scrollview, and set the scrollview as the view of the controller.
The problem is I can't see the image when I run the program, after clicking a row in the tableView (which is supposed to push the scrollview with the image in it). However, if I change tabss (in the tabbarviewcontroller) and come back to this tab. The image will show.
So I think the image download happens, but I somehow have a problem showing it instantly on the screen. It only appears after I come back to it. What do I seem to be doing wrong? I'm new to threads so I suspect it's a problem with that. Also my code was working before I made it so that it would do the download in another thread, so I'm pretty sure it is related to that.
This is the function in the Photo.m which is an Entity in Core Data. This is supposed to do the download
- (void)processImageDataWithBlock:(void (^)(NSData *imageData))processImage {
NSString *url = self.imageURL;
dispatch_queue_t callerQueue = dispatch_get_current_queue();
dispatch_queue_t downloadQueue = dispatch_queue_create("Flickr download", NULL);
dispatch_async(downloadQueue, ^{
NSData *imageData = [FlickrFetcher imageDataForPhotoWithURLString:url];
dispatch_async(callerQueue, ^{
processImage(imageData);
});
});
}
This is my loadView method in the PhotoScrollViewController.m
- (void)loadView {
[image processImageDataWithBlock:^(NSData *imageData) {
UIImage *imageToBeShown = [UIImage imageWithData:imageData];
imageView = [[UIImageView alloc] initWithImage:imageToBeShown];
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
scrollView = [[UIScrollView alloc] initWithFrame:applicationFrame];
scrollView.delegate = self;
scrollView.contentSize = imageToBeShown.size;
[scrollView addSubview:imageView];
self.title = image.title;
self.view = scrollView;
}];
}
Edited to add extra information that was added as an answer
Adding [super loadView] to my loadView method solved the issue. However, the documentation says that I shouldn't be calling super loadView from my loadView.
I tried moving the code to viewDidLoad and that works as well. But really this is the code that's setting the view so I feel like I should be putting it in loadView. But then it doesn't work when I use this multi threading mechanism for download.
Is this because the download is somehow interfering with me setting the view in loadView?
It seems loadView completes before the image download is done. You need some way of calling [imageView setNeedsDisplay] when image download is complete. You need some method that gets called when image download is done that accesses the imageView (perhaps using viewWithTag) from the scrollView and calls [imageView setNeedsDisplay];

Unable to run two OpenGL ES animations on two view controllers in an application

I was hoping for some help on this as I'm really stuck after trying to fix it for a few days.
Basically, my app has an OpenGL ES animation called levelsView that is displayed as soon as it has opened. Here is the code that starts my animation on the view controller:
- (void)animate
{
levelsView.animationInterval = 1.0 / 60.0;
[levelsView startAnimating];
[levelsView release];
}
The view controller also has a Switch button that lets the user change the animation. The code that displays the other animation is below:
- (IBAction) Switch: (id) sender {
SnowFallViewController* vce = [[[SnowFallViewController alloc] initWithNibName:#"SnowFallViewController"
bundle:nil] autorelease];
[self presentModalViewController:vce animated:YES];
}
Also here is the view for the code that starts the second animation on the second view controller:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect rect = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame:rect];
GLViewController *theController = [[GLViewController alloc] init];
self.controller = theController;
[theController release];
GLView *glView = [[GLView alloc] initWithFrame:rect];
[window addSubview:glView];
glView.controller = controller;
glView.animationInterval = 1.0 / kRenderingFrequency;
[glView startAnimation];
[glView release];
[window makeKeyAndVisible];
}
Basically, the problem I'm having is that I cannot get both the animations to work in the same application i.e. when the application is loaded up the first animation works but when the user clicks on the Switch button they only get a blank screen instead of the second animation.
One thing I've noticed is that the second animation will work if I do not start the first animation i.e. if I got in my animation code and delete
[levelsView startAnimating]; then the second animation will work(but obviously the first one will not).
Anyone with any insight on how I can fix this so I can get both animations to work?
Thanks,
Dave
Assuming you're using a CADisplayLink inside your GLView to run your animation loop, it seems from experience that they have mechanisms inside CADisplayLink to make sure that it doesn't just keep issuing calls upon calls if you're not keeping up with the refresh rate. It's more than possible that logic confuses itself a little if you have multiple CADisplayLinks attached at once.
I'd strongly suggest you add:
- (void)viewDidAppear:(BOOL)animated
{
[glView startAnimation];
}
- (void)viewWillDisappear:(BOOL)animated
{
[glView stopAnimation];
}
Those tie into the built-in mechanisms surrounding presenting and dismissing a view controller — the view controller is being told in the first instance when its view did appear (ie, the transition in is finished) and in the second when its view is about to disappear (ie, just before the transition outward begins). By stopping and starting animation on your GL view based on whether your controller is visible you'll save a lot of processing and prevent your disparate OpenGL views from fighting with each other for rendering times.