Adding Objects to View - ios

Im making a game of sorts, where balls are added to the screen. I need to be able to add custom balls (different speeds/images, ect) at time intervals (i.e. ball type 1 gets added every 15 seconds, whereas ball type 2 gets added every 30 seconds). I need these balls to be UIImageView (at least with the setup I have right now to check collisions).
Any ideas on how I can do this?
Also, How to make the player (a UIImageView) the start button. I have an IBAction that starts the game. I cannot connect the UIImageView and the IBAction though...

Only a partial answer first, but really you don't want to use UIImageView for user interaction. A UIButton can be set to the type 'custom' and then display an image (you can set the image in interface builder or programmatically):
btnImage = [UIImage imageNamed:#"image.png"];
[btnMyButton setImage:btnImage forState:UIControlStateNormal];
It is then much easier to pick up IBAction etc.
As regards creating the balls dynamically, creating a new UIImageView programmatically is easy using the initWithImage method, I suggest you read up on the UIImageView class reference at http://developer.apple.com/library/ios/ipad/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html.
UIImageView inherits from UIView so after creating each image, just add it as a sub view using something like:
[self.view addSubView:newImageView];
Regarding the timer element, NSTimer is the place to look for this. I don't have much experience using these but it should be straightforward. Reference can be found at:
http://developer.apple.com/library/mac/ipad/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html
Hth

Related

Animated Background

I am making an app in which there are two buttons in the center. The background is there and I need the background to have an animation. With that said I mean like little raindrops constantly falling in the background. I have no clue on how to do this. My customer really, really wants this. Thanks!
Make sure the images are named in numerical order with the number at the end of the name. Then drag and drop the file of images into your Xcode folder area. Then you reference only the image name and not the number in animatedImageNamed.
You need to create an ImageView the size of the Background for each device you plan on using.
//place this in your viewDidLoad
UIImageView * background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 55)];
UIImage * image = [UIImage animatedImageNamed:#"MovingCar" duration:3.6];
background.image = image;
[self.view setBackgroundView: background];
You can change the duration to suit your needs accordingly.
You could use the UIView method animateWithDuration:animations: or one of it's variants that has a completion method to animate images from the top to the bottom of the screen. Take a look at the Xcode docs on that method, and search the web for examples.
As the other poster says, this site is for helping people with problems, not for asking others to provide you with ready-made solutions.
I could bang out some animation code for you that would create an endless stream of falling raindrops, but it would probably take me a couple of hours to get it fully working, and I'd have to charge you for it. (Plus I'm not very familiar with Swift so I'd have to do some translation from Objective-C, which is my day-to-day programming language.)

Is it possible to add one UIButton object to multiple UIImageViews?

I have a scrollView and inside this scrollView, there are various PFImageView objects.
I need a single UIButton object on every PFImageView (same place and same button) that will load a subView with the information of the image tapped.
Is this possible?
So, let me get this straight... You want to create only one button, place it on multiple imageViews and have it respond differently (depending on which imageView was tapped)?
In this case...
No, you can't have 1 button in N places.
But... you can create N button objects and have them all assigned to 1 single target method in which... you can have different functionality based on the button's tag.
PS: UICollectionView would be cleaner approach.
You can't have a single control in multiple locations, it's that simple.
I saw your comment about not wanting to use a UICollectionView because it's not the design you're looking for, but I wonder what kind of design you want to create with UIScrollView only because UICollectionView doesn't works.
UICollectionView is not restrained to line/grid display of cells, but can be customized in a huge amounts of ways. You should watch the WWDC 2012 video "Introducing Collection Views" : at the end of it, some UICollectionView possibilities are shown and some of them are quite impressive.
Also, https://www.cocoacontrols.com/ website is a good place to find custom controls and even inspiration for your own design.
This is a little digression from the original issue, but I really think that a collection of UIImageView within an UIScrollView can be created with a UICollectionView, especially if you want to add some controls within your "cells".
Yes. You should use specific control events when adding actions to the button.
E.g. in my application i record/stop/cancel sound as long as user hold a button (drag out to cancel).. like a walkie talkie phone...
[recordButton addTarget:self action:#selector(recordStart) forControlEvents:UIControlEventTouchDown];
[recordButton addTarget:self action:#selector(recordStop) forControlEvents:UIControlEventTouchUpInside];
[recordButton addTarget:self action:#selector(recordCancel) forControlEvents:UIControlEventTouchDragExit];

Single Tapping not being detected on dynamically created uibuttons

It was necessary to create a subclass of UIButton, lets call it subButton. These subButtons are instantiated one by one and dynamically added to an UIScrollView object.
Unfortunately the subButtons aren't reacting as expected. After scrolling away from the first dynamic generated subButtons and then returning back to them. They loose their reactivity to single tapping. I can not explain this behaviour. Can somebody explain why its behaving this way?
Many thanks.
Source code
self.subButton = [[SubButton alloc]initWithFrame:CGRectMake(69.5, y, 201, 114)];
[self.subButton setBackgroundColor:[UIColor blueColor]];
self.subButton.imageData = imageData;
self.subButton.videoPath = videoPath;
self.subButton.vidIndex = _indexVideo +1;
[self.subButton drawVidPreview];
[self.subButton setTag:(_indexVideo +1)];
[self.subButton addTarget:self action:#selector(handleSingleTap:) forControlEvents:UIControlEventTouchUpInside];
[self.videoScroll addSubview:self.subButton];
[_videoPreviews addObject:self.subButton];
The subButton method drawVidPreview adds some subviews to itself.
Do the buttons not respond at all, or just not to single-tap? That might help us help you.
If they don't respond at all, I would check your view hierarchy to make sure no other view (perhaps invisible) are sitting above your buttons. Or, perhaps are the buttons not being added to the parent view you think?
Please make sure you are not adding the same UIGestureRecognizer instance to all of the UIButtons, instead for each UIButton you need to add a separate instance, however all gesture instance can point to the same target.
At last, i could solve the problem. The reason why the first subButtons did not respond to single tapping with UIControlEventTouchDown is because the uiScrollview content offset was set to a negative y value. This first of all was not necessary and was done out of pure laziness. I corrected it by giving it a positive value.
So my conclusion is, if you do not set the content offset or content size of the scrollView correctly, some dynamically added buttons to the scrollView might not respond to tapping appropriately. This is even the case if all Buttons are placed completely on a uiScrollview object.
This I could confirm by playing around with the content size of the uiScrollview instance too. I changed size of the y value of uiScrollviews instance just by few points and then some of the last buttons did not respond to tapping anymore.
Just by increasing the content size of the y value again, made the buttons responsive again.

How do I change an Image when a button is pressed?

Now I know this question may sound like a duplicate, but I doubt it seeing as I've researched and none of the possible duplicates answer my question. If it is a duplicate and it's been answered by all means show me the way!
But my question doesn't deal so much with a button changing an image, more so the button is the image.
So my button is an image and when pressed a sound plays and then the image changes until the sound is done. My problem is I can't get the image to change.
Now I have tried this method thinking that I could press my button and my new image would cover up my button until the sound was done:
UIImage *dolDerp = [UIImage imageNamed:#"dolphin2.png"];
[imageview setImage:dolDerp];
I have an outlet set up and it's connected to an image view object and then the action is connected to the button, so in theory when the button is pressed the new image should take over the screen. Now obviously the code needs to be tweaked so the button would go away after a few seconds when the sound is played, but my problem is I can't even get the button to display. I also would prefer to just have the button objects image change if possible?
If anyone could offer some help it's much appreciated!
the best thing is in Interface Builder assign the two images to the button Normal/Selected states.
in code just use this line:
myButton.selected = YES;
//or
myButton.selected = NO;
What #Mahmoud Fayez suggested is good. In code, the proper way to set a buttons image/background image is [button setImage:image forState:UIControlStateNormal] or the similar method for setBackgroundImage
If you are using IB though, it is indeed best to set the different images for different states in IB, then change the buttons state (which will then change the buttons image) in code.

Animate a switch using an image sequence

I would like to know how to solve a problem I'm having. I have an image sequence that basically animates a switch.
I'd like to implement this in iOS. How would you advise me to go for that? Maybe using UIButton? Or rather UISwitch?
For a rather simple solution (tap switches, no swiping possible), I would advise you to use two UIViews stacked.
-1- At the bottom, use a UIImageView which allows you to run image animations.
-2- At the top, use a UIControl which will capture the touché-up-inside events.
Alternatively, you could use a UITapGestureRecognizer as a replacement for -2-. The latter option should only be considered if the resulting control will not be used for iOS < 3.2.
As soon as your tap-recognizer (UIControl or UITapGestureRecognizer) receives the tap-event, you simply play the animation provided through your UIImageView.
For more details on animations using UIImageView see its class reference, specifically on things like animationImages (an array of images).
As UIImageView does not provide you with a reverse animation feature, you will have to assign the images in the reverse order before animating back to the start (that is after a user switched it on and now switches back to off).
For a more complex solution (tap switches, swiping is possible), you may add a UISwipeGesture Recognizer to your custom control and use the status of that UIGestureRecognizer to select the currently visible image of your UIImageView.
iOS 5 allows you to make animated UIImages. You can put the animated UIImage right into the UIButton and animate it on button presses. Animated UIImage

Resources