2009-07-17 16 views
5

मैं दो विचारों के बीच फ़्लिप करने की कोशिश कर रहा हूं। यह आसान है, कोड नीचे है, लेकिन मैं फ्लिप करने के लिए उपयोग किए गए बटन को एक साथ फ़्लिप करना चाहता हूं।आईफोन फ्लिप दाएं बटन (जैसे आईट्यून्स)

जब आप ट्रैक चला रहे हों तो आप इस व्यवहार को आईपॉड एप्लिकेशन में देख सकते हैं; कवर आर्ट और ट्रैक लिस्टिंग के बीच फ्लिप बटन फ्लिप को टैप करना, लेकिन यह एक ही समय में बटन को फ़्लिप करता है।

यह नेविगेशन नियंत्रक पर एक पृष्ठ है, और जिस बटन को मैं फ़्लिप करना चाहता हूं वह rightBarButtonItem है।

यहां मेरे पास अभी तक का कोड है। यह दृश्य को फिसलता है, लेकिन सही बार्बटन नहीं।

[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]; 

(छवि flipping यहाँ कोड संकलन नहीं हो सकता है, मैं इसे जोड़ा के बाद समझाने के लिए मुझे क्या करना कोशिश कर रहा था की कोशिश करना।)

मैं कहां से मुसीबत में चल रहा हूँ है मैं बदलना चाहते हैं नेविगेशन नियंत्रक में सबसे सही बटन तो यह एक साथ flips।

मैं यह कैसे कर सकता हूं? मैं क्या देख सकता हूं, और क्या मैं इसे एक ही एनीमेशन ब्लॉक के हिस्से के रूप में या अलग के रूप में करता हूं? किसी भी सुझाव की सराहना की जाएगी, मैं निश्चित रूप से अभी तक एनीमेशन पर एक अच्छा संभाल नहीं है।

उत्तर

5

कुछ चर्चा here है, लेकिन समाधान इतना सुरुचिपूर्ण नहीं है।

सबसे पहले, UIBarButtonItemUIView के वंशज नहीं हैं, तो आप संभवतः UIBarButtonItem पर UIKit एनिमेशन का उपयोग नहीं कर सकते हैं। हालांकि, आप customView सेट करने और उसे एनिमेट करने का प्रयास कर सकते हैं। आप एक ही एनीमेशन ब्लॉक का उपयोग कर सकते हैं।

+0

रातोंरात मुझे एहसास हुआ कि यह वास्तव में क्रेडिट देने से भी बेहतर विचार है। मुझे लगता है कि मैं UIImageView को देखने और बटन की तरह कार्य करने के लिए पर्याप्त छवियों को कैप्चर कर सकता हूं, जिसका अर्थ है कि मैं इसे खींच सकता हूं। धन्यवाद। –

2

ठीक है, क्या मैं वास्तव में इसे ठीक करने के लिए किया था है:

मैं पहले से ही एक कस्टम शीर्षक दृश्य का उपयोग किया गया था। rightBarButtonItem का उपयोग करने के बजाय, मैंने अपना कस्टम व्यू व्यापक बना दिया।

मैंने बटन के दोनों किनारों की एक छवि बनाई, नेविगेशन फ्रेम के साथ पूरा किया, और उन्हें एप्लिकेशन में एम्बेड किया। मेरी शीर्षक को देखते हुए, मैं डाल:

  • एक UIView कि सही नियंत्रण (यह rightControl कहते हैं) के लिए मेरे प्रतिस्थापन हो जाएगा, उचित रूप से तैनात।
  • UIView पर एक बटन जो UIControlEventTouchUpInside पर प्रतिक्रिया करता है और मेरे flipSide: को ट्रिगर करता है।

रनटाइम पर मैं प्रत्येक राज्य के लिए UIImageView बनाता हूं। मैंने एस rightControl में डाल दिया है, लेकिन डिफ़ॉल्ट रूप से छुपाएं। मैं एक समर्पित एनीमेशन ब्लॉक में flipSide: में छिपा झंडे स्विच करता हूं।

बेहद अजीब। लेकिन यह काम करता है।

+0

क्या आप इसके लिए कुछ कोड साझा कर सकते हैं? कुछ इसी तरह के साथ संघर्ष – arnoapp

2

बस सही नेविगेशन बटन के लिए एक कस्टम UIView का उपयोग करें जिसमें दो बटन शामिल हैं।

आप एक कस्टम UIView बनाने के सीधे आगे दृष्टिकोण का उपयोग कर सकते हैं जो सही नेविगेशन बटन आइटम के रूप में प्रदर्शित होता है। इस UIView में दो UIButtons शामिल होना चाहिए जिन्हें आप फ़्लिप करना चाहते हैं।याद रखें कि UIButtons UIViews भी हैं, इसलिए उन्हें उसी संक्रमण का उपयोग करके फ़्लिप किया जा सकता है, जिसमें एक सामान्य यूआई व्यू फ़्लिप किया जा सकता है और निश्चित रूप से उन्हें टैप किया जा सकता है! यहां कुछ नमूना कोड है जो काम करता है:

नोट: मैं UIViewController की कस्टम श्रेणी के रूप में बटन बनाने के लिए एक सुविधा फ़ंक्शन का उपयोग करता हूं (नोट: आप UIView के लिए कस्टम श्रेणी बनाने के लिए भी यही कोड जोड़ सकते हैं, बस इसे कॉपी करें लाइनों और UIViewController को UIView के साथ प्रतिस्थापित करें) - यदि आप इसका उपयोग करना चाहते हैं तो बस नीचे दिए गए कोड को शामिल करके एक कस्टम श्रेणी बनाएं, वैकल्पिक रूप से आप UIButtons को सामान्य रूप से बना सकते हैं।

// 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];*/ 
     }