2012-01-08 5 views
36

यह अपने आप ही केंद्र के चारों ओर एक दृश्य घुमाने के लिए संभव है?अपने स्वयं के केंद्रों के आसपास उप-विचारों को कैसे घुमाएं?

मेरे वर्तमान कार्यान्वयन में उप-दृश्य (बटन, आदि) एक अजीब मार्ग के किनारे ले जाने के लिए इससे पहले कि वे वांछित स्थिति में अंत लगते हैं (के बाद से सुपर दृश्य की समन्वय प्रणाली में घुमाया जाता है)। मैं विचारों को नीचे के चित्र में अपने स्वयं के केंद्रों के चारों ओर 90 डिग्री स्पिन करना चाहता हूं।

desired rotation

+21

अच्छी तरह से सचित्र प्रश्न के लिए +1 – Till

+0

कि सिर्फ एक उदाहरण में अनदेखी की है आप चाहते हैं स्थिति पट्टी अभी भी रहने के लिए, या है? – NJones

+0

@NJones हालांकि यह मेरी प्राथमिक उद्देश्य नहीं है, यह ठीक हो सकता है अगर यह अभी भी बने हुए हैं! मेरे वर्तमान कार्यान्वयन में, सबव्यूज़ "मजेदार पथ" के साथ आगे बढ़ते हैं क्योंकि सुपर व्यू (या रूट विंडो?) घूर्णन कर रहा है। – ragnarius

उत्तर

16

मुझे वास्तव में यह प्रश्न पसंद है। मुझे कुछ इंटरफेस के लिए रोटेशन एनीमेशन जारिंग भी मिलती है। यहां बताया गया है कि मैंने आपके द्वारा चित्रित किए गए कार्यों को कैसे कार्यान्वित किया है। एक साधारण @interface ठीक होगा। नोट: मैं एआरसी का उपयोग कर रहा हूँ।

#import <UIKit/UIKit.h> 
@interface ControllerWithRotatingButtons : UIViewController 
@property (strong, nonatomic) IBOutlet UIButton *buttonA; 
@property (strong, nonatomic) IBOutlet UIButton *buttonB; 
@property (strong, nonatomic) IBOutlet UIButton *buttonC; 
@end 

उचित आउटलेट कनेक्शन .xib में बटन के लिए बना रहे हैं:

enter image description here

ControllerWithRotatingButtons.m:

#import "ControllerWithRotatingButtons.h" 
@implementation ControllerWithRotatingButtons 
@synthesize buttonA = _buttonA; 
@synthesize buttonB = _buttonB; 
@synthesize buttonC = _buttonC; 
-(void)deviceRotated:(NSNotification *)note{ 
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; 
    CGFloat rotationAngle = 0; 
    if (orientation == UIDeviceOrientationPortraitUpsideDown) rotationAngle = M_PI; 
    else if (orientation == UIDeviceOrientationLandscapeLeft) rotationAngle = M_PI_2; 
    else if (orientation == UIDeviceOrientationLandscapeRight) rotationAngle = -M_PI_2; 
    [UIView animateWithDuration:0.5 animations:^{ 
     _buttonA.transform = CGAffineTransformMakeRotation(rotationAngle); 
     _buttonB.transform = CGAffineTransformMakeRotation(rotationAngle); 
     _buttonC.transform = CGAffineTransformMakeRotation(rotationAngle); 
    } completion:nil]; 
} 
-(void)viewDidLoad{ 
    [super viewDidLoad]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil]; 
} 
-(void)viewDidUnload{ 
    [super viewDidUnload]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; 
} 
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
@end 

और बस हो गया। अब जब आप अपने डिवाइस बारी बारी से स्क्रीन बारी बारी से होगा नहीं, लेकिन बटन के रूप में दिखाया जाएगा:

enter image description here

बेशक

अगर आप केवल बटन के लेबल चालू करने के लिए आप बस के बजाय _buttonA.titleLabel को बदलने लागू होगा चाहते हैं।

नोट: कृपया ध्यान दें कि एक बार डिवाइस के रूप में जहाँ तक बटन पर नहीं किसी भी छूता है का सवाल है घुमाया डिवाइस चित्र में अब भी है, लेकिन मेरी टिप्पणी करने के लिए आपकी प्रतिक्रिया है कि आप के लिए एक समस्या नहीं है इंगित करता है।

यदि आप एक संबंधित सवाल है एक टिप्पणी छोड़ने के लिए स्वतंत्र महसूस।

+0

बहुत बहुत धन्यवाद! कोड सुरुचिपूर्ण है और सबव्यूस अब इतनी धीमी गति से आगे बढ़ते हैं! – ragnarius

+0

अच्छी तरह से दोस्त बन गया! बहुत ही कुशल ! – Nil

4

आप manualy संपत्ति को बदलने का उपयोग कर किसी भी UIView उपवर्ग घुमा सकते हैं।

#import <QuartzCore/QuartzCore.h> 

myView.transform = CGAffineTransformMakeRotation(M_PI/2); 
+0

यह आसान लग रहा है! लेकिन किसी भी तरह से मुझे मुख्य दृश्य को घूर्णन से रोका जाना चाहिए, और पता लगाएं कि अपना कोड स्निपेट कहां रखा जाए। – ragnarius

+0

आपको 'UIViewController' रोटेशन कॉलबैक में देखना चाहिए, जैसे '- चाहिएऑटोरोटेट टूइंटरफेसऑरिएंटेशन:' और ' - willRotateToInterfaceOrientation: अवधि: '। देखें कि क्या आप दृश्य को रोटेशन से रख सकते हैं, लेकिन फिर भी रोटेशन ईवेंट की सदस्यता लें और तदनुसार अपने सबव्यूज़ को बदलें। –

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