UIView animation issue
I am very new to both Objective-c and Xcode. I am trying to implement a
simple UIPickerView + Toolbar setup that shows when a user taps on the
Label element on the right and then when a user selects an option from the
PickerView, the Label element gets replaced by the value of the selected
option.
Currently I have a UIPickerView and a Toolbar inside a UIVIew element. I
have got the slide up and down animation for the UIVIew to work. The
problem I am having is an issue with the positioning of the animation.
Please see screenshot:
When I click the 'Cancel' or 'Done' buttons on the toolbar above the
UIPickerView, it hides the whole UIView correctly but when I click the
'Show' button to bring the UIView up, it doesn't reposition high enough
for the whole UIView to be visible. Does anyone know why this is
happening?
When I force the UIView to be re-position higher by setting the animation
y axis value to 144 instead of 244, it looks like this:
It seems like the height of the UIPickerView is being changed during the
animation. How would I fix this?
Here is code from my .h and .m files:
.h
#import <UIKit/UIKit.h>
@interface NewInvoiceViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource> {
NSArray *storedClients;
NSString *selectedClient;
}
- (IBAction)cancel:(id)sender;
- (IBAction)clientSelectCancel:(id)sender;
- (IBAction)clientSelectDone:(id)sender;
- (IBAction)clientPickerShowBtn:(id)sender;
@property (strong, nonatomic) IBOutlet UIPickerView *clientPicker;
@property (strong, nonatomic) IBOutlet UILabel *clientLabel;
@property (strong, nonatomic) IBOutlet UIView *clientPickerViewContainer;
@end
.m
#import "NewInvoiceViewController.h"
@interface NewInvoiceViewController ()
@end
@implementation NewInvoiceViewController
@synthesize clientLabel, clientPicker, clientPickerViewContainer;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
storedClients = [NSArray arrayWithObjects:@"Joe Smith",@"John
Doe",@"Bob Johnson",@"Joel Smith", nil];
clientPickerViewContainer.frame = CGRectMake(0, 522, 320, 261);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)cancel:(id)sender{
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)clientSelectCancel:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
clientPickerViewContainer.frame = CGRectMake(0, 520, 320, 260);
[UIView commitAnimations];
}
- (IBAction)clientSelectDone:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
clientPickerViewContainer.frame = CGRectMake(0, 520, 320, 260);
[UIView commitAnimations];
}
- (IBAction)clientPickerShowBtn:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
clientPickerViewContainer.frame = CGRectMake(0, 244, 320, 260);
[UIView commitAnimations];
}
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [storedClients count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [storedClients objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
selectedClient = [storedClients objectAtIndex:row];
}
@end
Screenshot of Xcode storyboard: http://i.imgur.com/j8q6T9N.png I have
selected the UIView so that the position and dimension values are visible.
Tutorial used:
http://i-software-developers.com/2012/05/27/xcode-4-2-show-and-hide-pickerview/
Is this the best way to implement a setup like this? I have seen other
examples using something called an actionSheet, would it better to use
that?
actionSheet example: http://pastebin.com/5MC9JJC0
All help or advice is very much appreciated!
Thanks
No comments:
Post a Comment