2011-07-05 13 views
20

मेरे मुख्य दृश्य में, मैं कुछ इशारा करता हूं जिससे कुछ नया दृश्य दिखाना पड़ता है। इस समय मैं एक अच्छी यूआई अभ्यास के रूप में पूरी पृष्ठभूमि (इस नए दृश्य को छोड़कर) को मंद करना चाहता हूं। एचटीएमएल में, जावास्क्रिप्ट ऐसा लगता है - मैं आईओएस में यह वही प्रभाव कैसे प्राप्त करूं?आईओएस मंद पृष्ठभूमि जब एक दृश्य दिखाया गया है

enter image description here

उत्तर

33

पृष्ठभूमि के ऊपर एक UIView निर्धारित करना, [UIColor blackColor] करने के लिए अपनी पृष्ठभूमि सेट और 0.6 पसंद करने के लिए अपने अस्पष्टता निर्धारित किया है। फिर उस UIView को मूल दृश्य में जोड़ें।

यह पृष्ठभूमि को मंद कर देगा और पृष्ठभूमि नियंत्रण में किसी भी नल को रोक देगा।

+0

क्या आप इंटरफ़ेस बिल्डर में वह लेआउट कर सकते हैं, फिर प्रोग्रामिंग रूप से तब तक छुपाएं जब तक आप इसे नहीं चाहते? या छुपाए जाने पर भी यह हस्तक्षेप करेगा? – ToolmakerSteve

5

जबकि दान का सुझाव लक्ष्य पर है, आप इस मामले में एक मोडल व्यू कंट्रोलर का उपयोग नहीं कर सकते हैं क्योंकि एक सामान्य मोडल संपूर्ण स्क्रीन को कवर करता है, पैरेंट व्यू कंट्रोलर को अवरुद्ध करता है, भले ही आपके पास पारदर्शी के रूप में आपके दृश्य की पृष्ठभूमि हो (आप ' एप्लिकेशन के UIWindow में जो कुछ भी है उसे देखेंगे)।

यदि आप आईपैड पर ऐसा कर रहे हैं तो 2 मोडल प्रस्तुति शैलियों (UIViewModalPresentationStylePageSheet और UIViewModalPresentationStyleFormSheet) हैं जो कुछ समान कर सकते हैं, लेकिन वे आईफोन या आईपॉड टच पर काम नहीं करेंगे।

अंधेरे पृष्ठभूमि और आंशिक अस्पष्टता के साथ "छाया" दृश्य जोड़ें और दृश्य नियंत्रक के दृश्य पर सीधे जो भी दृश्य आप अग्रभूमि में होना चाहते हैं उसे जोड़ें। आप मानक UIView एनीमेशन ब्लॉक, या CoreAnimation का उपयोग करने में उन्हें एनिमेट कर सकते हैं।

एक और नोट, यदि आप उस छाया दृश्य को छूना चाहते हैं तो आप इसे touchesEnded: जैसे टच विधियों में से एक को ओवरराइड करने के लिए या एक यूआईसींट्रोल में बदल सकते हैं जो एक स्पर्श की घटनाओं को प्राप्त कर सकते हैं UIButton, लेकिन पाठ, छाया, राज्य आदि के लिए अतिरिक्त विकल्प के बिना

+3

आप सही हैं कि मैंने अपने जवाब में "मोडल" कहा था। मुझे वास्तव में किसी औपचारिक तरीके से "मॉड्यूल प्रस्तुत" दृश्य का अर्थ नहीं था। आप मूल रूप से अपना खुद का मोडल व्यू बना रहे हैं। –

1

उपरोक्त दो उत्तरों का काम तब होता है जब नए दृश्य की अपनी 'पृष्ठभूमि' होती है, यानी कोई पारदर्शिता नहीं होती है। समस्या जो मुझे यहां ले जाती है, इस तरह हल नहीं की जा सकती है, हालांकि, मैं जो करने की कोशिश कर रहा था वह यह है: मैं वास्तविक समय में वीडियो प्रदर्शित करने के लिए सामान्य AVCaptureVideoPreviewLayer के साथ अपनी स्क्रीन पर AVCaptureSession चला रहा हूं। मैं स्क्रीन का एक हिस्सा चुनना चाहता हूं (बाद में केवल इस भाग के साथ कुछ करने के लिए), और जब यह भाग चुना जाता है तो शेष वीडियो पूर्वावलोकन को मंद करना चाहते हैं। यह हैं कि यह दिखता है:

यह वह जगह है मैं यह कैसे हल: एक नया दृश्य जहां स्क्रीन को छुआ और वीडियो पूर्वावलोकन देखने के एक subview के रूप में जोड़ा जाता है के आधार पर बनाया जाता है। फिर, मैं स्क्रीन के बाकी हिस्सों को कवर करने के लिए पहले उल्लिखित पृष्ठभूमि रंग और अस्पष्टता के साथ 4 अतिरिक्त, गैर-ओवरलैपिंग आयताकार सबव्यूव्यू बनाता हूं। मुख्य दृश्य के सबव्यू होने के लिए मुझे इन सभी विचारों को स्पष्ट रूप से की आवश्यकता नहीं है, क्योंकि मुझे कहीं और स्क्रीन को स्पर्श करने और चयनित क्षेत्र को बदलने की क्षमता की आवश्यकता है।

मुझे यकीन है कि इसे हल करने के लिए और अधिक शानदार तरीके हैं, इसलिए अगर कोई जानता है कि कृपया कैसे टिप्पणी करें ...

1
#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController<UIGestureRecognizerDelegate> 
{ 
    UIView *mainView; 
    UIView *dimBackgroundUIView; 
    UIView *customView; 

    UIButton *btn; 
} 

@end 


#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    mainView = [[UIView alloc] init]; 
    mainView.backgroundColor = [UIColor whiteColor]; 
    [self.view addSubview:mainView]; 
    [self addConstraintsForMainView]; 

    btn = [[UIButton alloc] initWithFrame:CGRectMake(200, 200, 100, 30)]; 
    [btn setTitle:@"Button" forState:UIControlStateNormal]; 
    [btn setBackgroundColor:[UIColor blueColor]]; 
    [btn addTarget:self action:@selector(showCustomView_Action:) forControlEvents:UIControlEventTouchUpInside]; 
    [mainView addSubview:btn]; 

    dimBackgroundUIView = [[UIView alloc] init]; 
    dimBackgroundUIView.backgroundColor = [UIColor blackColor]; 
    dimBackgroundUIView.alpha = 0.2; 
} 

-(void)removeCustomViewsFromSuperView { 

    if(dimBackgroundUIView) 
     [dimBackgroundUIView removeFromSuperview]; 

    if(customView) 
     [customView removeFromSuperview]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)showCustomView_Action:(id)sender { 

    customView = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 80, 80)]; 
    customView.backgroundColor = [UIColor redColor]; 

    [self.view addSubview:dimBackgroundUIView]; 
    [self addConstraintsForDimView]; 

    [self.view addSubview:customView]; 

    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeCustomViewsFromSuperView)]; 
    tapped.delegate=self; 
    tapped.numberOfTapsRequired = 1; 
    [customView addGestureRecognizer:tapped]; 
} 

-(void) addConstraintsForDimView { 

    [dimBackgroundUIView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:0]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; 
} 

-(void) addConstraintsForMainView { 

    [mainView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:0]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; 
} 

@end 

ध्यान दें: आप Xcode पर ग्राफिकल इंटरफेस का उपयोग करके कोड का अनुकूलन कर सकते, लेकिन मैं यह

तो विचार है परीक्षण करने के लिए सक्षम होने के लिए प्रोग्राम के रूप में कोड लिखना पड़ा करने के लिए:

  1. सेट करने के बजाय पृष्ठभूमि रंग को ब्लैक और सेट अल्फा 0.1 या 0.2 या
  2. पर UIView नाम दें जिसे आप चाहते हैं (dimBackgroundUIView)
  3. जब आपको पृष्ठभूमि को मंद करने की आवश्यकता होती है तो कोड की यह 2 पंक्ति जोड़ें: [self.view addSubview: dimBackgroundUIView]; [स्वयं addConstraintsForDimView];
  4. और जब आप dimming पृष्ठभूमि को हटाना चाहते हैं तो कोड की यह 2 पंक्ति जोड़ें: यदि (dimBackgroundUIView) [dimBackgroundUIView removeFromSuperview];
संबंधित मुद्दे