2011-10-28 24 views
6

Tôi muốn tạo hoạt ảnh cho UIPickerView của mình sau khi nhấn nút. Tôi đã có mã hóa UIPickerView của tôi để được ẩn trên viewDidLoad và không ẩn sau khi nhấn một nút, nhưng nó không hoạt hình như thế nào một animal ModalViewController theo mặc định. Tôi chỉ muốn UIPickerView của tôi được hoạt hình giống như một animal ModalViewController theo mặc định.Làm cách nào để tạo hiệu ứng hiển thị UIPickerView sau khi nhấn nút

Tôi đã nghiên cứu trên trang web và trên web nhưng tôi dường như không thể làm điều đó đúng cách. Ai giúp tôi với?

Cảm ơn rất nhiều! :)

Dưới đây là mã của tôi:

#pragma mark - Picker View 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    return 4; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    timersArray = [[NSMutableArray alloc] init]; 
    [timersArray addObject:@"No timer"]; 
    [timersArray addObject:@"15 seconds"]; 
    [timersArray addObject:@"30 seconds"]; 
    [timersArray addObject:@"60 seconds"]; 

    return [timersArray objectAtIndex:row]; 
} 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    if ([[timersArray objectAtIndex:row] isEqual:@"No timer"]) 
    { 
     timerIndication.text = @"No timer selected"; 
     timersPickerView.hidden = YES; 
     // Animation code to dismiss picker should go here 
    } 
    else if ([[timersArray objectAtIndex:row] isEqual:@"15 seconds"]) 
    { 
     timerIndication.text = @"15 seconds selected"; 
     timersPickerView.hidden = YES; 
     // Animation code to dismiss picker should go here 
    } 
    else if ([[timersArray objectAtIndex:row] isEqual:@"30 seconds"]) 
    { 
     timerIndication.text = @"30 seconds selected"; 
     timersPickerView.hidden = YES; 
     // Animation code to dismiss picker should go here 
    } 
    else if ([[timersArray objectAtIndex:row] isEqual:@"60 seconds"]) 
    { 
     timerIndication.text = @"60 seconds selected"; 
     timersPickerView.hidden = YES; 
     // Animation code to dismiss picker should go here 
    } 
} 

#pragma mark - Delay method 

// This is where Send button should be enabled 
- (IBAction)selectTimer 
{ 
    timersPickerView.hidden = NO; 
    // Animation code to present picker view should go here 
} 

Trả lời

10

Bạn có thể sử dụng đoạn mã sau cho sinh động quan điểm chọn sau khi nhấn nút:

-(IBAction)button:(id)sender 
{ 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.6]; 
    CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, 200); 
    PickerView.transform = transfrom; 
    PickerView.alpha = PickerView.alpha * (-1) + 1; 
    [UIView commitAnimations]; 

} 

Đừng quên để thêm đoạn mã sau vào phương pháp viewDidLoad bạn

PickerView.alpha = 0;  
[self.view addSubview:PickerView]; 

Những gì nó là nó làm cho xem người xem rơi từ trên cùng của màn hình vào lần nhấp chuột đầu tiên và để làm cho xem bộ chọn biến mất, bạn chỉ có thể nhấp vào nút một lần nữa. Từ nhấp chuột tiếp theo xem bộ chọn chỉ xuất hiện và biến mất .. Hy vọng nó giúp và hoạt động :)

+1

Câu trả lời tuyệt vời :). – androniennn

+0

@androniennn Vui mừng khi câu trả lời của tôi hoạt động và cảm ơn vì sự bổ sung của bạn :) –

5

Bạn có thể tạo một viewController và sau đó thêm UIPickerView bên trong bộ điều khiển và sau đó sử dụng:

[tự presentModalViewController: viewControllerWithPickerView hoạt hình: YES];

hoặc bạn có thể thêm UIPickerView của bạn không ẩn nhưng với giá trị ay lớn hơn màn hình, như 480.0 hoặc lớn hơn và sau đó sử dụng hoạt ảnh UIView để di chuyển UIPickerView từ vị trí đó đến vị trí hiển thị trên màn hình, có thể mô phỏng ModalViewController hoạt hình.

+0

Cảm ơn. Thay vào đó, chỉ cần thực hiện giải pháp thay thế này. ;) – jaytrixz

+0

Bất kỳ workaround cũng làm cho xem mờ đi khi tôi tải modalviewcontroller của tôi với pickerview trong nó? – jaytrixz

+0

Bạn có thể sử dụng UIView ẩn với nền đen và alpha là 0.5 và lắng nghe hoạt hình UIPickerView của bạn, khi nó hoàn thành thì bạn cho thấy rằng UIView. UIView này nên được đặt trên tất cả mọi thứ khác nhưng UIPickerView. –

0

Khi bạn nhấn nút, hãy thực hiện thao tác [self.view addSubVuew: yourPickerView]; . Nó có hoạt động không?

+0

Nó không phải là trường hợp. Khi tôi nhấn nút, nó chỉ làm cho PickerView xuất hiện vì nó được ẩn theo mặc định trong viewDidLoad. Nó ẩn một lần nữa sau khi chọn một hàng trên PickerView. – jaytrixz

2

Bạn có thể sử dụng chức năng dưới đây để thực hiện hoạt ảnh của chế độ xem bộ chọn.

-(void)hideShowPickerView { 

if (!isPickerDisplay) { 
    [UIView animateWithDuration:0.25 animations:^{ 
     CGRect temp = self.categoryPicker.frame; 
     temp.origin.y = self.view.frame.size.height - self.categoryPicker.frame.size.height; 
     self.categoryPicker.frame = temp; 
    } completion:^(BOOL finished) { 
     NSLog(@"picker displayed"); 
     isPickerDisplay = YES; 
    }]; 
}else { 
    [UIView animateWithDuration:0.25 animations:^{ 
     CGRect temp = self.categoryPicker.frame; 
     temp.origin.y = self.view.frame.size.height; 
     self.categoryPicker.frame = temp; 
    } completion:^(BOOL finished) { 
     NSLog(@"picker hide"); 
     isPickerDisplay = NO; 
    }]; 
} 

}

Xác định isPickerDisplay như BOOL và khởi tạo này trong để viewDidLoad như được đưa ra dưới đây -

isPickerDisplay = YES; 
[self hideShowPickerView]; 

này sẽ quản lý ẩn và hiển thị chức năng cho UIPickerView.

1

Giải pháp Swift 3

Tôi đồng ý với @EshwarChaitanya, thuộc tính alpha có thể hoạt hình và đây là cách sử dụng phù hợp.

override func viewDidLoad() { 
    super.viewDidLoad() 

    timersPickerView.alpha = 0 
} 

@IBAction func selectTimer() { 
    UIView.animate(withDuration: 0.3, animations: { 
     self.timersPickerView.alpha = 1 
    }) 
} 

Tôi không chắc chắn cách bạn muốn ẩn nó để tôi sẽ để nó tùy thuộc vào bạn.

Các vấn đề liên quan