2011-07-05 26 views
20

Trong chế độ xem chính của tôi, tôi thực hiện một số hành động cử chỉ khiến một số chế độ xem mới được hiển thị. Tại thời điểm này tôi muốn làm mờ toàn bộ nền (ngoại trừ chế độ xem mới này) làm thực hành giao diện người dùng tốt. Trong HTML, Javascript có vẻ như vậy - Làm thế nào để có được hiệu ứng tương tự trong iOS?Nền mờ của iOS khi chế độ xem được hiển thị

enter image description here

Trả lời

33

Lay một UIView trên nền, thiết lập nền tảng của nó để [UIColor blackColor] và thiết lập Opacity của nó để thích 0.6. Sau đó thêm UIView vào chế độ xem gốc.

Điều này sẽ làm mờ nền và chặn bất kỳ thao tác nhấn nào vào điều khiển nền.

+0

Bạn có thể thực hiện bố cục đó trong trình tạo giao diện, sau đó chỉ ẩn theo chương trình cho đến khi bạn muốn? Hoặc sẽ bằng cách nào đó can thiệp ngay cả khi bị ẩn? – ToolmakerSteve

5

Trong khi đề xuất của Dan được nhắm mục tiêu, bạn không thể sử dụng bộ điều khiển chế độ xem trong trường hợp này vì một phương thức điển hình bao phủ toàn bộ màn hình, chặn bộ điều khiển chế độ xem gốc, ngay cả khi bạn có nền của chế độ xem trong suốt (bạn ' sẽ thấy bất cứ điều gì bạn có trong UIWindow của ứng dụng).

Nếu bạn đang thực hiện việc này trên iPad, có 2 kiểu trình bày phương thức (UIViewModalPresentationStylePageSheetUIViewModalPresentationStyleFormSheet) có thể làm tương tự, nhưng những thứ này sẽ không hoạt động trên iPhone hoặc iPod Touch.

Thêm chế độ xem "bóng", với nền tối và độ mờ một phần và bất kỳ chế độ xem nào bạn muốn ở nền trước, trực tiếp vào chế độ xem của trình điều khiển chế độ xem. Bạn có thể tạo ảnh động cho họ bằng cách sử dụng các khối hoạt ảnh chuẩn UIView hoặc CoreAnimation.

Một lưu ý khác, nếu bạn muốn chặn chạm vào chế độ xem bóng đó, bạn có thể biến nó thành nút khổng lồ, phân lớp UIView để ghi đè một trong các phương pháp chạm như touchesEnded: hoặc thay đổi nó thành UIControl. UIButton, nhưng không có tùy chọn bổ sung cho văn bản, bóng tối, tiểu bang, v.v.

+3

Bạn nói đúng rằng tôi đã nói "phương thức" trong câu trả lời của tôi. Tôi đã không thực sự có nghĩa là một cái nhìn "trình bày một cách khiêm tốn" trong bất kỳ cách thức chính thức nào. Bạn đang xây dựng chế độ xem phương thức của riêng mình, về cơ bản. –

1

Hai câu trả lời trên hoạt động nếu chế độ xem mới có 'nền' riêng, nghĩa là không có độ trong suốt. Vấn đề dẫn tôi đến đây không thể được giải quyết theo cách đó, những gì tôi đang cố gắng làm là: Tôi đang chạy AVCaptureSession trên màn hình của mình với thông thường AVCaptureVideoPreviewLayer để hiển thị video theo thời gian thực. Tôi muốn chọn một phần của màn hình (để sau đó làm một cái gì đó chỉ với phần này), và khi phần này được chọn muốn làm mờ phần còn lại của xem trước video. Đây là những gì nó trông giống như:

Đây là cách tôi giải quyết này: Một quan điểm mới được tạo ra tùy thuộc vào nơi màn hình được chạm vào và thêm vào như là một subview của xem xem trước video. Sau đó, tôi tạo thêm 4 phần phụ hình chữ nhật bổ sung, không chồng chéo với màu nền và độ mờ đục được đề cập trước đây để che phần còn lại của màn hình. Tôi rõ ràng cần tất cả các chế độ xem này không phải là các bản xem trước của chế độ xem chính vì tôi cần khả năng chạm vào màn hình ở một nơi khác và thay đổi khu vực đã chọn.

Tôi chắc chắn có nhiều cách thanh lịch hơn để giải quyết vấn đề này, vì vậy nếu ai đó biết cách xin vui lòng nhận xét ...

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 

lưu ý: bạn có thể tối ưu hóa mã bằng cách sử dụng giao diện đồ họa trên Xcode nhưng tôi đã phải viết mã lập trình để có thể kiểm tra nó

để ý tưởng là:

  1. tạo UIView đặt tên cho thứ bạn muốn (dimBackgroundUIView) so với đặt màu nền là Đen và đặt alfa thành 0,1 hoặc 0,2 hoặc ....
  2. thêm 2 dòng mã này khi bạn cần làm mờ nền: [self.view addSubview: dimBackgroundUIView]; [tự thêmConstraintsForDimView];
  3. và thêm 2 dòng mã này khi bạn muốn xóa nền mờ : if (dimBackgroundUIView) [dimBackgroundUIView removeFromSuperview];
Các vấn đề liên quan