2009-07-17 44 views
5

Tôi đang cố lật giữa hai chế độ xem. Thật dễ dàng, mã bên dưới, nhưng tôi cũng muốn đồng thời lật nút được sử dụng để thực hiện thao tác lật.Nút lật phải iPhone (như iTunes)

Bạn có thể thấy hành vi này trong ứng dụng iPod khi bạn đang phát một bản nhạc; chạm vào nút lật lật giữa bìa nghệ thuật và danh sách bản nhạc, nhưng nó sẽ lật nút cùng một lúc.

Đây là trang trên bộ điều khiển điều hướng và nút tôi muốn lật là rightBarButtonItem.

Đây là mã tôi có cho đến thời điểm này. Điều này lật chế độ xem, nhưng không phải là nút rightBarButton.

[UIView setAnimationBeginsFromCurrentState: YES]; 
[UIView setAnimationDuration: 0.5f]; 
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; 
showingBackside = !showingBackside; 
if (showingBackside) { 
    [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft 
          forView: self.view 
          cache: YES]; 
    [self.view addSubview: backside.view]; 
    [frontside.view removeFromSuperview]; 
} else { 
    [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight 
          forView: self.view 
          cache: YES]; 
    [self.view addSubview: frontside.view]; 
    [backside.view removeFromSuperview]; 
} 
// flip image, too 
NSString *newImage = showingBackside ? @"backside.png" : @"frontside.png"; 
[(self.navigationItem.rightBarButtonItem) setImage: [UIImage imageNamed: newImage]]; 
[UIView commitAnimations]; 

(Mã hình ảnh lật ở đây có thể không biên dịch, tôi thêm vào nó sau khi cố gắng giải thích những gì tôi đã cố gắng làm.)

Nơi tôi đang chạy vào rắc rối là tôi muốn thay đổi nút ngoài cùng bên phải trong bộ điều khiển điều hướng để nó lật đồng thời.

Làm cách nào để thực hiện việc này? Tôi xem hoạt hình nào, và tôi có làm điều đó như là một phần của cùng một khối hình động hay là một hình ảnh riêng biệt không? Mọi lời khuyên sẽ được đánh giá cao, tôi chắc chắn không có một xử lý tốt trên hình ảnh động nào được nêu ra.

Trả lời

5

Có một số cuộc thảo luận here, nhưng giải pháp không quá thanh lịch.

Trước hết, kể từ UIBarButtonItem không phải là hậu duệ của UIView, bạn có thể không thể sử dụng hoạt ảnh UIKit trực tiếp trên UIBarButtonItem. Tuy nhiên, bạn có thể thử thiết lập customView và làm hoạt hình đó. Bạn có thể sử dụng cùng một khối hình động.

+0

Nghỉ đêm Tôi nhận ra đây thực sự là một ý tưởng hay hơn tôi đã ghi nhận tín dụng. Tôi nghĩ rằng tôi có thể chụp đủ hình ảnh để tạo ra giao diện UIImageView và hoạt động giống như một nút, điều đó có nghĩa là tôi có thể xóa hình ảnh này. Cảm ơn. –

2

Được rồi, đây là những gì tôi thực sự đã làm để sửa lỗi này:

tôi đã sử dụng một cái nhìn tiêu đề tùy chỉnh. Thay vì sử dụng rightBarButtonItem, tôi đã làm cho chế độ xem tùy chỉnh của mình rộng hơn.

Tôi đã tạo hình ảnh của cả hai mặt của nút, hoàn chỉnh với khung điều hướng và nhúng chúng vào ứng dụng. Theo quan điểm của tiêu đề của tôi, tôi đặt:

  • Một UIView đó sẽ là sự thay thế của tôi cho quyền kiểm soát (gọi nó là rightControl), vị trí thích hợp.
  • Nút trên UIView phản hồi UIControlEventTouchUpInside và kích hoạt flipSide: của tôi.

Khi chạy, tôi tạo một UIImageView cho mỗi trạng thái. Tôi đưa ra UIImageView s trong số rightControl nhưng ẩn mật khẩu không phải là mặc định. Tôi chuyển các cờ ẩn xung quanh trong flipSide: trong một khối hoạt hình riêng.

Thật lạ lùng. Nhưng nó đã có tác dụng.

+0

Bạn có thể chia sẻ một số mã cho điều này không? Đấu tranh với một cái gì đó tương tự – arnoapp

2

Chỉ cần sử dụng UIView tùy chỉnh cho nút điều hướng phù hợp có chứa hai nút để lật giữa.

Bạn có thể sử dụng phương pháp tiếp cận thẳng về tạo UIView tùy chỉnh được hiển thị dưới dạng mục nút điều hướng phù hợp. UIView này nên chứa hai UIButtons bạn muốn lật giữa.Hãy nhớ rằng UIButtons là UIViews quá để họ có thể được lộn bằng cách sử dụng cùng một quá trình chuyển đổi mà một giao diện người dùng bình thường có thể được lộn với và tất nhiên họ có thể được khai thác! Dưới đây là một số mã mẫu hoạt động:

Lưu ý: Tôi sử dụng chức năng tiện lợi để tạo các nút làm danh mục tùy chỉnh của UIViewController (lưu ý: bạn có thể thêm cùng mã này để tạo danh mục tùy chỉnh cho UIView. dòng và thay thế UIViewController với UIView) - Nếu bạn muốn sử dụng nó quá chỉ cần tạo một thể loại tùy chỉnh bằng cách bao gồm mã này dưới đây, luân phiên bạn có thể tạo UIButtons như bình thường.

// create custom category for UIViewController to allow common button creation routine, add to .m or .mm file or add to a .h file and #import that .h file 
     @interface UIViewController (ButtonAndSelector) 
     - (UIButton *)createUIButtonWithImage:(UIImage *)image forState:(UIControlState)state withSelector:(SEL)selector usingFrame:(CGRect)buttonImageFrame; 
     @end 

     @implementation UIViewController (ButtonAndSelector) 
     - (UIButton *)createUIButtonWithImage:(UIImage *)buttonImage forState:(UIControlState)state withSelector:(SEL)selector usingFrame:(CGRect)buttonImageFrame 
     { 
      UIButton *button = [[UIButton alloc] initWithFrame:buttonImageFrame]; 
      [button setBackgroundImage:buttonImage forState:state]; 
      [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside]; 
      [button setShowsTouchWhenHighlighted:YES]; 
      return button; 
     } 
     @end 

// add this to your .h file: 
     @property (strong, nonatomic) UIView *coverListView; 
     @property (strong, nonatomic) UIButton *listButton; 
     @property (strong, nonatomic) UIButton *coverButton; 

     - (void)animateCoverListButtonFlip; 

// add this to your .m or .mm file to synthesize the variables: 
     @synthesize coverListView; 
     @synthesize listButton; 
     @synthesize coverButton; 

// add this to your .m or .mm file in the viewDidLoad: 

     // setup right button bar (flips between list icon and coverart image) 
     self.coverListView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 32, 30)]; 
     self.coverListView.backgroundColor = [UIColor clearColor]; 
     self.coverListView.opaque = NO; 

     self.listButton = [self createUIButtonWithImage:[UIImage imageNamed:@"navbar_icon_tracklisting"] forState:UIControlStateNormal withSelector:@selector(showHideQueue) usingFrame:CGRectMake(0, 0, 32, 30)]; 
     self.listButton.backgroundColor = [UIColor clearColor]; 
     self.listButton.showsTouchWhenHighlighted = NO; 

     self.coverButton = [self createUIButtonWithImage:[UIImage imageNamed:@"default_coverart_small"] forState:UIControlStateNormal withSelector:@selector(showHideQueue) usingFrame:CGRectMake(0, 0, 32, 30)]; 
     [self.coverListView addSubview:self.coverButton]; // show coverButton by default 
      self.coverButton.showsTouchWhenHighlighted = NO; 

     UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.coverListView]; 
     [self.navigationItem setRightBarButtonItem:barButtonItem]; 


// add this to viewDidAppear if you want to flip the button when the screen appears like the build in iPod app does 
     [self animateCoverListButtonFlip]; 

// add this routine to flip the right navigation bar custom view/buttons 
     - (void)animateCoverListButtonFlip 
     { 
      [UIView beginAnimations:nil context:NULL]; 
      [UIView setAnimationDuration:0.75];  
      [UIView setAnimationTransition:([self.listButton superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight) forView:self.coverListView cache:YES]; 

      if ([self.listButton superview]) { 
       [self.listButton removeFromSuperview]; 
       [self.coverListView addSubview:self.coverButton]; 
      } else { 
       [self.coverButton removeFromSuperview]; 
       [self.coverListView addSubview:self.listButton]; 
      } 
      [UIView commitAnimations]; 
     } 

// when the playing album cover changes, remember to update the coverButton: 
     UIImage *artworkImage; // set this to the current playing album image 
     [self.coverButton setImage:artworkImage forState:UIControlStateNormal]; 

// don't forget to call the animateCoverListButtonFlip in the button click handler (shown above as showHideQueue) that shows and hides the cover/queue(list of album tracks0 - something like this: 

     - (void)showHideQueue 
     {  
      [self animateCoverListButtonFlip]; 

      /* replace the code below that is commented out here with your own code that transitions between your cover view and your list view of album tracks, this code shows my transition and references views that are not part of this example/answer, but you don't need those - you'll have your own cover view (musicPlayerView) and list view (musicQueueView) to flip between. 
      [UIView beginAnimations:nil context:NULL]; 
      [UIView setAnimationDuration:0.75]; 
      [UIView setAnimationTransition:([musicQueueView superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight) forView:self.contentView cache:YES]; 

      if ([musicQueueView superview]) { // if music queue is displayed 
       [musicQueueView removeFromSuperview]; 
       [self.contentView addSubview:musicPlayerView]; 
      } else { 
       [musicPlayerView removeFromSuperview]; 
       [self.contentView addSubview:musicQueueView]; 
       [[musicQueueView queueTableView] reloadData]; 
      } 
      [UIView commitAnimations];*/ 
     } 
Các vấn đề liên quan