2

我对 Objective-c 和 Xcode 都很陌生。我正在尝试实现一个简单的 UIPickerView + 工具栏设置,当用户点击右侧的 Label 元素时显示,然后当用户从 PickerView 中选择一个选项时,Label 元素将被所选选项的值替换。

目前我在 UIVIew 元素中有一个 UIPickerView 和一个工具栏。我有 UIVIew 工作的上下滑动动画。我遇到的问题是动画的定位问题。请看截图:

UIView 动画问题

当我单击 UIPickerView 上方工具栏上的“取消”或“完成”按钮时,它会正确隐藏整个 UIView,但是当我单击“显示”按钮以启动 UIView 时,它的重新定位不够高UIView 可见。有谁知道为什么会这样?

当我通过将动画 y 轴值设置为 144 而不是 244 来强制 UIView 重新定位更高时,它看起来像这样:

UIView动画问题2

UIPickerView 的高度似乎在动画期间发生了变化。我将如何解决这个问题?

这是我的 .h 和 .m 文件中的代码:

。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

Xcode 故事板的屏幕截图:http: //i.imgur.com/j8q6T9N.png 我选择了 UIView 以便位置和尺寸值可见。

使用的教程:http: //i-software-developers.com/2012/05/27/xcode-4-2-show-and-hide-pickerview/

这是实现这样的设置的最佳方式吗?我已经看到使用称为 actionSheet 的其他示例,使用它会更好吗?

actionSheet 示例:http ://pastebin.com/5MC9JJC0

非常感谢所有帮助或建议!

谢谢

4

2 回答 2

1
  1. 尝试将动画语法与块一起使用,它们比您使用的这种旧方式要好得多

  2. 硬编码要设置动画的帧的值不是一个好习惯。相反,请尝试将您的框架与某些东西相关联,例如父母的框架。

使用类似的东西来显示你的选择器:

[UIView animateWithDuration:.5 animations:^{

    CGRect newFrame = clientPickerViewContainer.frame;
    newFrame.origin.y = self.view.frame.size.height - newFrame.size.height; 
    clientPickerViewContainer.frame = newFrame;

} completion:nil];

要隐藏它,只需将行更改newFrame.origin.y = self.view.frame.size.height - newFrame.size.height;newFrame.origin.y = self.view.frame.size.height;

即使您在 Interface Builder 中更改框架的值,或者您使用的是 3.5 或 4 英寸显示器,这种方式也可以工作。

我不知道问题是否只是这个,但值得指出。

于 2013-08-23T02:09:47.360 回答
1

如果您使用自动布局,那么您应该避免使用框架来布局视图。

您有一个选择器到屏幕顶部的约束,尝试为该布局的常量属性设置动画以向上和向下移动视图。文档中有它的例子。

于 2013-08-23T07:32:30.620 回答