创建一个重叠的半透明工具栏按钮,不会被工具栏一分为二

时间:2015-02-12 01:28:27

标签: ios uitoolbar

我创建了一个用户界面,其中包含一个位于工具栏顶部的绿色超大相机启动/停止按钮:

UI Screenshot

到目前为止,我一直在使用yoichitgy/EEToolbarCenterButton来实现这一点,而这似乎就是我最初想要的。

UI设计师特别要求按钮透明。他们还希望它背后的工具栏是"切出"以这种方式,你不会看到按钮后面的工具栏。这是按钮应该如何工作的说明。

This is an illustration of how the button should work.

以下是他们不想要的例子:

This is an illustration of what I'm not supposed to do.

那么无论如何都要这样做?哦,是的,还有一件事:一旦按下这个绿色按钮,它就会变成一个"停止"按钮小得多。停止按钮不应该是半透明的,并且它后面的条不应该缺少块。换句话说,我需要能够打开和关闭此行为。

1 个答案:

答案 0 :(得分:0)

我最终通过创建自定义UIToolbar类来解决此问题:

DLYCustomToolbar.h

#import <UIKit/UIKit.h>
#import "UIToolbar+EEToolbarCenterButton.h"

@interface DLYCustomToolbar : UIToolbar

@property (nonatomic) bool isMasked;

@end

DLYCustomToolbar.m:

#import "DLYCustomToolbar.h"

@implementation DLYCustomToolbar

- (void) drawRect:(CGRect)rect {
    [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5] setFill];
    UIRectFill(rect);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetBlendMode(context, kCGBlendModeClear);

    if(_isMasked) {
        float circleDiameter = 58;
        CGRect pathRect = CGRectMake(self.frame.size.width/2-circleDiameter/2, -circleDiameter/2+2, circleDiameter, circleDiameter);
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:pathRect];
        [path fill];
    }

    CGContextSetBlendMode(context, kCGBlendModeNormal);
}

- (void) setIsMasked:(bool)isMasked {
    if (isMasked != _isMasked) {
        _isMasked = isMasked;
        [self setNeedsDisplay];
    }
}

@end
相关问题