Use addTarget:action:forControlEvents: method to change a UIImage? - objective-c

I am intending to add a "blank" star icon on my page which will be changed to a "solid" star when a user clicks on it.
I tried to set the UIImage as shown below:
UIImage *hotIcon = [UIImage imageNamed:#"blank_star.png"];
Can anyone advise me how I can use the addTarget:action:forControlEvents: method? Can I even use this method on a UIImage? Or must I definitely use a UIButton?

You can add a method to action against a button like following:
[aButton addTarget:self action:#selector(myMethod:) forControlEvents:UIControlEventTouchUpInside];

"addTarget: action:" and "removeTarget: action:" is for all UIControls (like UIButton). There is no way you can use it for UIImages directly.
There are multiple ways to achieve your requirement. (declare a custom UIControl and implement the way you want). But simplest approach is to use UIButton with required images (and background images).

Related

AddTarget to UIView

I'm writing my own custom control which is implemented through UIView.
And when I initialize it, I would like to add a custom target to it like this:
[myControl addTarget:self action:#selector(turnOn) forControlEvents:UIControlEventValueChanged];
I do this so when I change something on my UIView, it would trigger that sort of method in its parent control. I am aware that there's a delegate for such purposes, but I was wondering if it is possible to do without it.
How do you do this sort of thing in Objective-C? Thanks in advance!
If you instead subclass UIContol you can invoke
- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents
from within your subclass, and that will trigger the action

iOS - How to control UIBezierPath drawing with UISlider?

I want to control the drawing of my UIBezierPath using a UISlider.
The aim is to be able to scrub the timeline (UISlider) and cause the line (UIBezierPath) to draw based on the current position of the slider.
So that when I scrub forward the path renders forward & when I scrub back the path renders backwards.
I have the code to render the path drawing in both directions based on this sample code which I added reverse path to (https://github.com/ole/Animated-Paths)
The main thing I am struggling to understand is how can I have the path render when I move the slider since I cant really use animations as they would not update with the sliders movement..
Cheers
Damien
If you are tracking the "Control Events" of the UISlider, for example like this:
[yourSlider addTarget:self action:#selector(seekBegan:) forControlEvents:UIControlEventTouchDown];
[yourSlider addTarget:self action:#selector(seekContinues:) forControlEvents:UIControlEventTouchDragInside];
[yourSlider addTarget:self action:#selector(seekEnded:) forControlEvents:UIControlEventTouchUpInside];
.. you can then do your animations inside "seekContinues:". I can't say how smooth it will be though! as there may be a lot of calls to that method.
So I found that I was unable to use animations to achieve what I wanted so I used a custom UIView subclass and implemented in its drawRect method the code to handle the animations
Basically I set a index in the subclass that controlled how many of the points the drawrect method draws.
This gave me the desired approach and allowed me to change the value based on the slider.
For the drawing I used CGContextMoveToPoint & CGContextAddLineToPoint to render the paths
Cheers

How does UIBarButtonItem know which UIBarStyle the UIToolBar uses?

In Xcode ui builder when one set the UIBarStyle of the UIToolBar (such as BlackTranslucent, for example), the UIToolBarItem matches the background images to it. How does the UIToolBarItem know which style it should use?
I'm trying to do a put a custom colored image on top of the regular background tile (programatically merge a given image on top of the background image). I want to the code to be generalizable enough so that it is able to handle all UIBarStyles. That means I want to know when UIToolBarItem decides which background to use and intercept it so I can compose the button image on the fly.
Without being able to see apple's implementation of UIBarbuttonItem, I posit that they do not, in fact, know what the style of their UIToolbar is. If you look closely, they have the same alpha as their toolbar, and the same overlay (indicative of a subview). Therefore, any image that is below this highlighted layer, added as a subview, should conform to the UIToolbar's style. If you want to use multiple images though (one for each barStyle), you can determine it with self.myToolbar.barStyle and plan appropriately at -viewDidLoad time. As for true image drawing, subclass UIToolbar and override -drawRect: and use [UIImage drawInRect:rect];

Can I use IOS5 Appearance in only some cases, to preserve the back arrow?

I am using the appearance protocol [[UIBarButtonItem appearance] setTintColor:[UIColor blueColor]]; to color all my buttons. The problem I am running into is that there are some rare cases where I have to create a custom back button like so, UIButton* customBack = [UIButton buttonWithType:101];, inorder to have it callback to an event when pressed. When I use the appearance protocol it removes the arrow shape.
Does anyone know a way to get back the arrow shape? Or is there a way to specify in the appearance to not effect a certain class?
The only accepted way I know of is to use an image. Also, I think you should be using [UIButton buttonWithType:UIButtonTypeCustom] instead of the numeric value.

Change UIButton type programmatically

I have an UIButton made with IB, it is set to "Rounded Rect". At one point, I'd like to change the type to "Custom" in the code, is that possible ? I saw the type can be set at creation, but did not see if it was possible to change it later on.
If I'm not mistaken, different button types are represented by different (private) subclasses of UIButton. That's why you can only set type at creation time but not after.
I don't know if I'll be much help, but for me it worked by doing:
button = [UIButton buttonWithType:(UIButtonType)];
example:
calculateButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];