Initiate a UIPicker from a UIbutton - xcode

I would like to make a UIbutton, by press it, it trigger the appearance of a UIPicker from bottom with animation.
May I know how and what code can i initiate the UIpicker from button? And where should i place and set the UIpicker in Interface Builder? and lastly, after choose from the picker, how can it move away?
Many thanks!

You need to look up some documentation on how to make a UIPickerView. There are tons of tutorials out there, even video tutorials. Just search around. It's part of becoming a good programmer.
I personally would set up my picker in interface builder, and set up an IBOutlet to it. I would then, in the .h file of your viewController, set up a method called
-(IBAction)showPicker
Them link the method to your button.
In your .m file of your viewController you should have
-(IBAction)showPicker
{
[UIView beginAnimations];//tells the compiler to start a new animation
[UIView setAnimationDuration:1.0];//in seconds
thepicker.frame = CGRectMake(100, 100, 200, 200);//move the picker to a specific position
[UIView commitAnimations];//tells the compiler to start the animations
}
the beginAnimations and commitAnimations is the code that says any code between us will be animated. So you can animate their alpha (transparency), their positions, and others as well. thepicker refers to your IBOutlet name, whatever you called your picker. I would move the picker out of the screen in your interface builder, then when you press the button, move it into postion with
thepicker.frame = CGRectMake(x,y,width,height);
then, to move it away, just use the same code but change the frame to be out of the screen when you animate it.
BTW, I would create your pickerView first in the viewdidload method, instead of creating it on tapping the button. Otherwise, you will constantly be creating UIPickerViews everytime the button is hit.

Related

How to animate the insert of a UIBarButtonItem

I have a simple app with a Tool Bar at the bottom. Depending upon the context of what the user is doing, I may want various buttons visible or not in the Tool Bar. So, in my code, I remove the weak modifier for the IBOutlet reference to those buttons, and I remove the non-visible buttons in viewDidLoad. Great.
Now, when the context changes, I add/remove buttons as desired using
toolBar.items?.removeAtIndex(0)
toolBar.items?.insert(playButton, atIndex: 0)
Great, works well. But for aesthetics, I would like to have the button removal/insert animated. Apparently the animated:YES argument from Objective-C days is gone. Do you know, does the API support animation of the button shuffling? Or, if not, how best to accomplish this?
Thanks!

Highlight NSImageView in NSView

I have a simple question. I have my NSView which is detecting drops (drag and drop). When user drops a link with image from browser, I detect that action, create NSImageView, initialize it on a place where user dropped it with some default frameSize and put the image from the link into it.
I would now like to highlight that NSImageView when user clicks on it. I also want to implement moving around that NSImageView in NSView but I'm pretty sure I will manage that. How do I highlight that exact NSImageView which was clicked? I haven't created earlier that NSImageView in interfacebuilder and assigned a special class for it so I can use drawRect, I have just created it dynamically...
Any help would be appreciated.
You can use NSImageView's setImageFrameStyle for highlighting.

Moving a UIButton programmatically?

Right now I want to move the position of the button dependant if it is an iPhone 5 screen or not, but I am having trouble figuring out how to move the button, right now I have an "if statement" that can figure out if it is an iPhone 5 or not, but I don't know how to move my button inside the statement. Take a look at my code.
.h
-(IBAction)randomButton;
//the button I want to move
.m
- (void)viewDidLoad
{
[super viewDidLoad];
if (IS_IPHONE_5) {
//I don't know how to move the button in here.
}
}
The right place to put this programmatic layout is either in a layoutSubviews method (if, for example, the view controller's root view is a custom subclass) or in the viewDidLayoutSubviews method of your view controller class. In either of those methods, you can set the button's position and size by modifying its frame, like:
_theButton.frame = /*whatever*/
If you want to change the button's position without modifying the button's size you can adjust the button's center instead of its frame, but usually if you're doing programatic layout you want to set both the position and size in the same place.
If you need more complex layouts, you can also use 2 XIB files, one for iPhone 5 and one for iPhone 3/4. I typically do that as I can always see what it will look like without guessing or trial and error.

Adding a NSButton to a CALayer in OS X Cocoa

I have a layer hosting view containing a CAlayer, displaying in this case a nice blue opaque rectangle. What I want to do is to add a NSButton on top of the layer, so that it sits above and moves with the blue rectangle when it is animated.
My attempt so far is as follows:
in #interface
IBOutlet NSButton* firstButton;
in #implementation
[layer addSublayer:[firstButton layer]];
firstButton.layer.position=NSMakePoint(0, 80.);
This successfully moves the location of the button on screen, but it doesn't move the 'hit target' of the button.
According to the similar question asked here on Apple Mailing Lists the solution seems to be to move the NSButton with a setFrameOrigin: on the button. This doesn't seem to work for me as it changes the position of the displayed button as well as the 'hit target'. I can't seem to move the hit target independently.
Alternatively: Am I going about this all the wrong way? Is there a better way of doing this?
Unfortunately you cannot move buttons (including the hit target) by manipulating their layers. This is highly unfortunate, but you're going to have to use the animator proxy on the frame of the button itself, and not try to modify the layer directly.

NSPopUpButtonCell inside custom cell does not pop up when clicked

I've created a custom subclass of NSCell with an NSImageCell, some NSTextFieldCells, and an NSPopUpButtonCell.
I'm initializing the pop up cell using:
myPopUpCell = [[NSPopUpButtonCell alloc] init];
[myPopUpCell setBordered:NO];
[myPopUpCell setAutoenablesItems:NO];
[myPopUpCell addItemsWithTitles:[NSArray arrayWithObjects:#"Item1", #"Item2", #"Item3"]];
And drawing it in drawInteriorWithFrame:inView:
Everything seems to work great, except that when clicking on the pop up cell while running my app the cell does not pop up. Any suggestions about what might be wrong?
Drawing the pop-up button cell in drawInteriorWithFrame:inView: is going to do just that; draw it, but nothing else. Handling click events is unrelated to drawing, so you're going to have to do some work in your custom cell to interpret mouse events, and if they're inside the frame you're using for the pop-up button, pass them on to the button cell. Start by subclassing the methods listed in the NSCell docs under tracking the mouse, such as –trackMouse:inRect:ofView:untilMouseUp:, and you should be be able to figure out what's needed to make the button cell act correctly.
Depending on what you're doing you may actually find it easier to draw the title string yourself, and just use NSMenu's +popUpContextMenu:withEvent:forView:.

Resources