2015-03-07 11 views
5

मैं आईओएस के लिए AndroidSlidingUpPanel जैसे कुछ ढूंढ रहा हूं। मुझे MBPullDownController मिला, लेकिन इसे दो व्यू कंट्रोलर का उपयोग करने की आवश्यकता है, और उस ऐप के आर्किटेक्चर में एक बड़ा बदलाव की आवश्यकता है जिसे मैं कार्यान्वित करने के लिए काम कर रहा हूं।Google मानचित्र ऐप की तरह एक स्लाइडिंग पैनल कैसे बनाएं?

मुझे बस ऐसा कुछ चाहिए जो किसी मौजूदा व्यू कंट्रोलर में सबव्यूव जोड़ता हो। मुझसे यह कैसे होगा?

उत्तर

3

मैं अपने आईओएस ऐप्स में पैनलों को स्लाइड करने का बहुत उपयोग करता हूं और मुझे पता चला है कि चाल स्टोरीबोर्ड (या xib फ़ाइल) में दृश्य नियंत्रक को कस्टम दृश्य जोड़ने के लिए है, लेकिन इसकी फ्रेम सेट करने के लिए स्क्रीन से बाहर है। आप यह सुनिश्चित कर सकते हैं कि दृश्य layout constraints का उपयोग कर किसी भी डिवाइस पर स्क्रीन बंद रहता है।

फिर यह उचित होने पर स्क्रीन पर दृश्य को एनिमेट करने का एक मामला है। उदाहरण के लिए:

- (IBAction)showPanel:(id)sender 
{ 
    // panelShown is an iVar to track the panel state... 
    if (!panelShown) { 
     // myConstraint is an IBOutlet to the appropriate constraint... 
     // Use this method for iOS 8+ otherwise use a frame based animation... 
     myConstraint.constant -= customView.frame.size.height; 
     [UIView animateWithDuration:0.5 animations:^{ 
      [self.view setNeedsLayout]; 
     }]; 
    } 
    else { 
     myConstraint.constant += customView.frame.size.height; 
     [UIView animateWithDuration:0.5 animations:^{ 
      [self.view setNeedsLayout]; 
     }]; 
    } 
} 

तुम सिर्फ एक कड़ी चोट ऊपर/नीचे करना चाहते हैं और उस पैनल तुम इतनी तरह UISwipeGestureRecognizer उपयोग कर सकते हैं का पता चलता है, तो:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // iVar 
    swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)]; 
    swipeUp.direction = UISwipeGestureRecognizerDirectionUp; 
    [self.view addGestureRecognizer:swipeUp]; 

    // Do the same again with swipeDown using UISwipeGestureRecognizerDirectionDown... 
} 

- (void)didSwipe:(UIGestureRecognizer *)swipe 
{ 
    if (swipe == swipeUp) { 
     // Show panel (see above)... 
    } else { 
     // Hide panel (see above)... 
    } 
} 

पैनल की तरह अपनी उंगली को ट्रैक करने के यदि आप चाहते हैं जब आप आईओएस पैनल दिखाते हैं (वाईफाई को बंद आदि पर चालू करने के लिए)। तो आप UIPanGestureRecognizer का उपयोग कर सकते हैं और translationInView: और velocityInView: प्राप्त कर सकते हैं और तदनुसार पैनल को समायोजित कर सकते हैं। यहाँ है कि उंगली आंदोलन को ट्रैक करता है, लेकिन एक UIViewController में touchesBegan:withEvent:- (void)touchesMoved:withEvent: और - (void)touchesEnded:withEvent: तरीकों का उपयोग कर आप एक स्वाद देने के लिए कोड का एक टुकड़ा है:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // Don't worry too much about buttonView this is another view that I animate upwards to get out the way of the panel as it slides in from the left... 
    [super touchesBegan:touches withEvent:event]; 
    CGPoint loc = [[touches anyObject] locationInView:self.view]; 
    // Save last touch for reference... 
    lastTouch = loc; 
    // leftBeginRect is an area where the user can start to drag the panel... 
    // trackFinger defines whether the panel should move with the users gestures or not... 
    if (CGRectContainsPoint(leftBeginRect, loc) && canTrack) { 
     trackFinger = YES; 
    } 
    // Left view is a reference to the panel... 
    else if (leftView.frame.size.width >= 300) { 
     // This means that the panel is shown and therefore should track the user's finger back towards the edge of the screen... 
     CGRect frame = CGRectMake(250, 0, 100, self.view.frame.size.height); 
     if (CGRectContainsPoint(frame, loc) && canTrack) { 
      trackFinger = YES; 
     } 
    } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesMoved:touches withEvent:event]; 
    CGPoint loc = [[touches anyObject] locationInView:self.view]; 
    // Need to work out the direction in which the user is panning... 
    if (lastTouch.x > loc.x) { 
     currentFingerDirection = RHFingerDirectionLeft; 
    } 
    else { 
     currentFingerDirection = RHFingerDirectionRight; 
    } 
    lastTouch = loc; 
    if (trackFinger) { 
     if (loc.x <= 300) { 
      // This means that the panel is somewhere between fully exposed and closed... 
      // This is where the frame for the left view (and the constraints) are adjusted according to the user's current finger position... 
      CGRect frame = leftView.frame; 
      frame.size.width = loc.x; 
      [leftView setFrame:frame]; 
      leftViewConstraint.constant = loc.x; 
      if (loc.x <= 80) { 
       float percentage = loc.x/80; 
       int amount = 100 * percentage; 
       CGRect otherFrame = buttonView.frame; 
       otherFrame.origin.y = -amount; 
       [buttonView setFrame:otherFrame]; 
       constraint.constant = constraintConstant + amount; 
      } 
     } 
     else { 
      CGRect frame = leftView.frame; 
      frame.size.width = 300; 
      [leftView setFrame:frame]; 
      leftViewConstraint.constant = 300; 
      frame = buttonView.frame; 
      frame.origin.y = -100; 
      [buttonView setFrame:frame]; 
      constraint.constant = constraintConstant + 100; 
      trackFinger = NO; 
     } 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // This method works out if the panel should pop open or spring closed when the user ends the gesture... 
    [super touchesEnded:touches withEvent:event]; 
    if (trackFinger) { 
     CGPoint loc = [[touches anyObject] locationInView:self.view]; 
     if (loc.x >= 50 && currentFingerDirection == RHFingerDirectionRight) { 
      CGRect frame = leftView.frame; 
      frame.size.width = 300; 
      leftViewConstraint.constant = 300; 
      CGRect otherFrame = buttonView.frame; 
      otherFrame.origin.y = -100; 
      constraint.constant = constraintConstant + 100; 
      [UIView animateWithDuration:0.2 animations:^{ 
       [leftView setFrame:frame]; 
       [buttonView setFrame:otherFrame]; 
      }]; 
     } 
     else if (loc.x <= 250 && currentFingerDirection == RHFingerDirectionLeft) { 
      CGRect frame = leftView.frame; 
      frame.size.width = 0; 
      leftViewConstraint.constant = 0; 
      CGRect otherFrame = buttonView.frame; 
      otherFrame.origin.y = 0; 
      constraint.constant = constraintConstant; 
      [UIView animateWithDuration:0.2 animations:^{ 
       [leftView setFrame:frame]; 
       [buttonView setFrame:otherFrame]; 
      }]; 
     } 
     else if (loc.x <= 150) { 
      CGRect frame = leftView.frame; 
      frame.size.width = 0; 
      leftViewConstraint.constant = 0; 
      CGRect otherFrame = buttonView.frame; 
      otherFrame.origin.y = 0; 
      constraint.constant = constraintConstant; 
      [UIView animateWithDuration:0.2 animations:^{ 
       [leftView setFrame:frame]; 
       [buttonView setFrame:otherFrame]; 
      }]; 
     } 
     else { 
      CGRect frame = leftView.frame; 
      frame.size.width = 300; 
      leftViewConstraint.constant = 300; 
      CGRect otherFrame = buttonView.frame; 
      otherFrame.origin.y = -100; 
      constraint.constant = constraintConstant + 100; 
      [UIView animateWithDuration:0.2 animations:^{ 
       [leftView setFrame:frame]; 
       [buttonView setFrame:otherFrame]; 
      }]; 
     } 
     trackFinger = NO; 
    } 
    currentFingerDirection = RHFingerDirectionNone; 
} 

कोड काफी शामिल है, लेकिन यह एक अच्छा पैनल एनीमेशन कि इस प्रकार में जो परिणाम आपके आईओएस पैनल की तरह बहुत उंगली।

+0

मुझे नहीं पता कि आप की कमी के आउटलेट बना सकता था। क्या आपके पास पैन इशारा पहचानकर्ता का उपयोग करने के लिए भी इसका एक उदाहरण है? या एक उदाहरण परियोजना? धन्यवाद – swanhella

+0

मेरे पास एक उदाहरण प्रोजेक्ट नहीं है लेकिन मैं एक इशारा पहचानकर्ता शामिल करने के लिए अपना उत्तर अपडेट करूंगा। , केंद्र फ्रेम, सीमा: –

+0

चियर्स मैं इसे अगर कोई रुचि है एक कोशिश – swanhella

1

देर से प्रतिक्रिया के लिए खेद है। मैंने रोब के जवाब के आधार पर एक छोटा सा lib बनाया। https://github.com/hoomazoid/CTSlidingUpPanel

यह उपयोग करने के लिए काफी सरल है:

@IBOutlet weak var bottomView: UIView! 
var bottomController:CTBottomSlideController?; 


override func viewDidLoad() { 
    super.viewDidLoad() 
    //You can provide nil to tabController and navigationController 
    bottomController = CTBottomSlideController(parent: view, bottomView: bottomView, 
        tabController: self.tabBarController!, 
        navController: self.navigationController, visibleHeight: 64) 
    //0 is bottom and 1 is top. 0.5 would be center     
    bottomController?.setAnchorPoint(anchor: 0.7) 
} 
+0

+1 इसे लाइब्रेरी में बनाने और इसे पूरी तरह से दस्तावेज करने के लिए काम और प्रयास के लिए +1। बमर यह स्विफ्ट में है: / – Stunner

संबंधित मुद्दे