2011-12-27 2 views
2

Пожалуйста, проверьте изображение ниже. Я добавил scrollview в цвет «BLACK» и добавил subview в цвете «GREY». теперь я хочу сделать прозрачным subview, который определяется как цвет «WHITE».Сделать пересечение прозрачным, используя несколько подпунктов

См. Приведенный ниже код. Позвольте мне знать, как сделать кнопку прозрачной с определенным фреймом или сообщить мне, если у вас есть альтернатива для этого.

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 40.0, self.frame.size.width, 300.0)]; 
self.scrollView.contentSize = CGSizeMake(self.bounds.size.width,ViewHeight); 
self.scrollView.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
self.scrollView.delegate = self; 
self.scrollView.backgroundColor =[UIColor blackColor]; 
[self.view addSubview:self.scrollView 


UIButton *butApp = [UIButton buttonWithType:UIButtonTypeCustom]; 
[butApp setFrame:CGRectMake(x, y , w, h)]; 
[butApp setBackgroundColor:[UIColor greyColor]]; 
[self.scrollView addSubview:butApp]; 


UIButton* gapButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[gapButton setFrame:CGRectMake(x+3, y+10, w-6, 10)]; 
[gapButton setBackgroundColor:[UIColor whiteColor]]; 
[self.scrollView addSubview:gapButton]; 

Вместо этого gapButton мне нужно прозрачная часть в сером цвете, чтобы пользователь мог видеть черный цвет в этой части.

enter image description here

+0

Вы пробовали [UIColor clearColor]? – johnluttig

+0

Да, я пробовал clearColor на gapButton, но он показывает мне серый цвет, но мне нужен черный цвет в этой белой части. –

+0

устанавливает свой альфа-свойство на 0. это может помочь вам – iPhone

ответ

0

Попробуйте это, я предложил это в комментариях. Другой ответ требует подкласса UIButton, который, по вашему мнению, не был идеальным в вашей ситуации.

UIButton *butApp = [UIButton buttonWithType:UIButtonTypeCustom]; 
[butApp setFrame:CGRectMake(x, y , w, h)]; 
//[butApp setBackgroundColor:[UIColor greyColor]]; //replace this... 
[butApp setBackgroundImage:[UIImage imageNamed:@"greyWithHoleInCenter.png"] 
        forState:UIControlStateNormal]; //...with this 
[self.scrollView addSubview:butApp]; 

Я создал грубый файл .png, чтобы представить вид backgroundImage, который вы, возможно, ищете. Обратите внимание, что центр прозрачный, а не белый. Таким образом, он должен показать все, что изображение находится за ним:

button with hole

0

Ok. Смешной вопрос. На мой взгляд, вы должны переопределить метод drawRect: + drawInContext: путем подкласса класса UIView. Кроме того, необходимо установить вид контейнера (серый вид) + кнопка BG для clearColor

-(void)drawRect:(CGRect)r 
{ 
// Let this method blank will cause drawLayer:InContext to be called 
} 

-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context 
{ 
     // Fill the bg with gray 
    CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor); 
    CGContextFillRect(context, self.bounds); 

     // I clear the subview frames 
    for (UIView * v in [self subviews]) // I do it for all subviews 
    { 
     CGContextSetBlendMode(context, kCGBlendModeClear); 
     CGContextFillRect(context, v.frame); 
    } 
} 

here is a screen shot

Смежные вопросы