Add new UIButton "state" with custom image - ios

I'm trying to build a simple peg jumping game (see: http://cl.ly/2x0v1V3z351t0j3d3y0L).
My plan is to make each "peg" a UIButton and toggle the UIButton states for the different images that need to be displayed at different times during the game. Using the default states for UIButton gets me pretty far but I need to add one extra state beyond the default 4.
How would I go about doing this? I figure I will need to sub class UIButton to create a custom UIButton object but this is where I get stuck. I can't find any resources on adding new button states. Is this something that is possible?

This link below was a major help to me implementing my own custom button. I basically took that guy's code and made it ARC compatible, but the basic principle in what he's done really works great!
Custom Button States

Subclass UIBUtton and add a custom state variable, then use the setter to change the image that is shown.

Related

How do I link my button to the selected state?

Now, I have sort of asked this question already, but this time it's a little different. Since my last question, I have received several answers and excellent advice, and I did some extensive research as well. I am using xcode, and coding for iOS 6.
My question specifically is how do I keep my button in a temporary "selected" state when pressed? Like I want it to stay pressed through the duration of the sound and then switch back to the default image.
My button is mostly linked up through interface builder, because it's easy and less code to write. I have the sound coded in my button's IBAction to play when pressed and I have the second image that it changes to when pressed, however the Image isn't set in my code it is set in Interface Builder.
I've seen several code that seems like it would work wonders if I knew how to correctly input it. Which leads me to a second part of my question.
Can someone help me set it up?
for example:
[btnClear setBackgroundImage:[UIImage imageNamed:#"blue_button.png"]
forState:UIControlStateHighlighted];
This seems like it would do the trick in my head. I just don't know which variable of mine to replace "btnClear" with and setBackgroundImage isn't showing up as a command.
On my other question I received and answer in the form of a comment that made the most sense but they didn't elaborate, it was:
"you can set image for Selected state of your button, and on touchUpInside set property selected = YES.
So I have the image I want in the selected state, and my button is linked for touchUpInside, but the only "property" or action it has is to play the sound, and because my button's images are set in Interface Builder I'm not really sure how I can set selected = YES without coding a new class which sets up the image.
Now if that's what I have to do, that's fine, but I need some guidance setting that up I am highly familiar with Objective-C it's just I'm new to these default classes in the iOS SDK I'm still getting a hold of things and having to refer to tutorials every now and again.
Here's a link to my other question if it helps:
How do I make a button stay pressed?
Again, I already have everything set up regarding the Image in Interface Builder, I'm just not sure how to fit the selected state option it into my already existing button action.
Thank you!
I think what you may be looking for is a button property:
#property (nonatomic, weak) IBOutlet UIButton *button;
(Or with a more descriptive name!) Hook this up to your button in Interface Builder. Then when you want to switch to the selected image (which you said is already in IB, right?), simply do this:
self.button.selected = YES;
And to turn back to the default image:
self.button.selected = NO;

Is there a way to create a UIButton-like frame in iOS without the button?

I'm working on an iPad app right now that takes as input an XML file and outputs a Cocoa-like user interface that's dynamically generated based on the content of the XML file. Part of the specification that I've been given dictates that one of the UI elements is a simple frame that looks exactly like a Cocoa Touch button, but does not act as a button; IE, it does nothing when clicked on, and does not visually change in any way. I wanted to use the iOS equivalent of a NSBox mocked up to look like a NSButton, but upon inspection it seems that there is no UIBox class that is equivalent to a NSBox the same way that UIButton is similar/equivalent to the NSButton. Can anyone more experienced in iOS than I suggest a design pattern for creating a button's 'frame' without the additional weight of a new button?
Simple, just disable user interaction on the button. This way it will still look like a button but it won't act like one.
[myButton setUserInterActionEnabled:NO];
Alternatively you could create a UIImageView and use Quartz to adjust the corner radius of the image.
#import QuartzCore/QuartzCore.h
[[myImageView layer] setCornerRadius:15.0];

Create custom UIButton with multiple images and shift Label and maybe add properties

for an iPad application in ios5.0 and arc, I need to create a button that has an image covering the entire button, and needs to have another transparent image at the bottom half of this button image OR have the button text label covering the bottom half of this button image.
In posts on this site I've read that using button subclass to just change the appearance of the UiButton should not be done. However, if I don't subclass, can I add these transparent image/and shift the button label? if so, how?
In case I need to add properties to the button, what is the best way to go about it.
If subclassing is the only option, can you also pls give pointers on what are the methods that i must absolutely override and any other such memory/performance considerations that I must keep in mind
Pointers to Any tutorials or third party libraries would be most appreciated.. Thanks in advance for all your help
I don't agree on "subclassing UIButton is not good". That's exactly why inheritance and subclassing mechanisms exist. In all platforms, the framework provides a base foundation for general needs, and you do extend them in the case standard stuff does not satisfy your needs. And you do it by subclassing.
As long as you know what you do, and what you do works for you and solves your problem, you're fine.
When you subclass UIButton, depending on what you actually want to achieve, you may want to override init:, initWithRect:, layoutSubviews:, awakeFromNib: methods.
Inspecting some subclasses would also help:
https://github.com/ardalahmet/SSCheckBoxView
https://github.com/ardalahmet/CopyableCell
For UIButton, you can inspect this component. Source code may help a lot.

How can I create a dynamic overlay for a UIButton?

My application has a few portions that have really big buttons (640x130, 230x150, etc.) What I need is to have a way to update different portions of the button, with different text. Initially, I assumed that in my code I could create various UILabels and then add them as subviews to my button. However, as soon as I try to add a UILabel to the button as a sub-view, my app crashes.
What is the easiest way to create an overlay for a button, that I can completely layout myself, without preventing button taps from being interested using overlay controls?
I imagine there are multiple ways to solve this problem. However, the best solution for my case should use the fewest lines of code (I have quite a few of these types of buttons) and I'd like to be able to continue using some form of configurable button within IB.
I'm not opposed to subclassing UIButton but, if I do, I would like to be able to use it in IB. I've never created a custom UIView for such a circumstance, so I'd need help defining that type of subclass so that it will work correctly in IB.
You need to add the subview to the containing view - not the button. To ensure that is doesn't interfere with button presses, be sure to set it to:
[myCustomTextOverlay setUserInteractionEnabled:NO];

How to make custom buttons in iOS?

The default iOS UI is nice but if I wanted to use images for buttons instead how would I do that - is it OpenGL?
Make the button a "custom button" type in interface builder. Then set the background image to be whatever you want the background to be.
Unfortunately the default button style is a little boring. You'll need to find or make a button with an image editor.
UIButton has a lot of support for custom buttons. In the nib (or storyboard or whatever), just click on the button and set its "Type" to "Custom" in the attribute inspector on the right side of the screen. Then, just below that, set the State Config to the sate you'd like (default, selected, etc) to customize, and set Image attribute to the image you'd like.
That image must be part of the project first before it will show up in the Image attribute (or in the app). You can add the image to the project simply by dragging it into the file navigator on the left side of the screen.
You can use images for buttons using a few simple lines of code:
// code to set image for button at normal state
[myButton setImage:[UIImage imageNamed:#"buttonImage.png"]
forState: UIControlStateNormal];
You can also use Xcode to change the type to "Custom" then you can set images for each button "state."
You can read more about UIButton states here
You can simply use PNG files that you create yourself. The UIButton class allows you to use custom images for all the different states of the application.
You don't really need to do custom rendering yourself, but if you really want, you can use UIKit to draw the images yourself in code (no OpenGL involved): http://developer.apple.com/library/ios/#documentation/2ddrawing/conceptual/drawingprintingios/graphicsdrawingoverview/graphicsdrawingoverview.html
You can do it by setting image or backgroundImage properties of a UIButton.
If you want your button to look nice when the size changes, you can use stretchableImageWithLeftCapWidth:topCapHeight: method of UIImage, with an appropriate image.
You may also want to use a custom component if it satisfies your need.
its very easy
change this two thing.Change "type" round rectangle to custom and drag image to project and select it as background .Run and Go
Well so far i was too looking for using custom buttons and the graphics, I've come through this site they have some awesome tutorial which was quite helpful for me. I've watched all the video tutorial and read some books but nothing helped me but this site and their 4 books are quite awesome and really helpful to read and understand it well.
http://www.raywenderlich.com/27191/learn-to-code-ios-apps-4-making-it-beautiful

Resources