【问题标题】:UIView animation issueUIView 动画问题
【发布时间】:2013-08-23 01:17:22
【问题描述】:

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

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

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

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

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

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

谢谢

【问题讨论】:

    标签: ios objective-c uiview uipickerview


    【解决方案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 英寸显示器,这种方法也可以工作。

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

    【讨论】:

    • 感谢您的建议。我已经更改了我的动画语法并且它正在工作,但是当 UIView 处于“向上”位置时,它的位置仍然存在问题。更新的 .m 文件:pastebin.com/iVT56M13 应用截图:i.imgur.com/QGebHSF.png
    • 问题还是一样(部分UIView没有显示)?如果是,那可能是自动布局的问题,因为您正在使用它(根据屏幕截图)。你熟悉吗?尝试为您的选择器和容器视图设置固定高度的约束,尝试降低将视图绑定到顶部/底部等的优先级。
    • 非常感谢!!我在 UIView 和 UIPickerView 元素上添加了一个固定的高度约束,现在它可以正确显示:) 我如何设置它,以便在 ViewController 首次加载时 UIview 处于“向下”(不可见)位置?
    • 我看到您在不同按钮的两个操作中重复隐藏代码。只需使用动画代码创建一个新方法hide,然后在您的操作中调用它。您也可以在您的viewWillAppear 或viewDidLayoutSubviews 中调用此hide 方法
    • 默认情况下它们不会出现。只需实现其中一种方法,就像它是你的一样。这叫做覆盖。 -(void)viewDidLayoutSubviews {...}
    【解决方案2】:

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

    您有一个将选择器限制在屏幕顶部的约束,请尝试为该布局的常量属性设置动画以向上和向下移动视图。文档中有相关示例。

    【讨论】:

    • 哦,我明白了,您能否提供指向包含这些示例的文档的特定部分的链接。
    • 我在一个小型设备上,所以这并不容易。 WWDC 2012 中有三个出色的自动布局视频涵盖了这一点,包括动画约束的示例。我鼓励您花时间观看它们,或者至少查看相关的 PDF 幻灯片。
    • 好的,没问题。很酷,我会找到并观看这些视频并阅读 PDF 幻灯片。感谢您的帮助:)
    • 感谢您之前的建议,我现在正在动画约束而不是框架。这是我更新后的 .m 代码:​​pastebin.com/1a1BqVGG 这是您推荐我这样做的方式吗?
    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多