透明覆盖视图仅在首次打开应用程序时打开

时间:2012-07-30 23:36:26

标签: iphone objective-c ios

我相信我有一些代码可以确定我的应用程序是否已在开始之前打开了:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL didFirstLaunch = [defaults boolForKey:@"DidFirstLaunch"];
if (!didFirstLaunch) {
    [defaults setBool: YES forKey:@"DidFirstLaunch"];

    //Code for assembling overlay view
}

我把这段代码放在didFinishLaunchingWithOptions方法中。

我希望透明覆盖视图仅覆盖iPhone屏幕的3/4并且略微透明,这样您仍然可以看到我的应用程序的主页面 - 它上面会有几张图片和文字(基本上一个简单的教程)。 如何创建这种性质的简单视图?

2 个答案:

答案 0 :(得分:4)

您应该将代码放在didFinishLaunchingWithOptions方法中:

BOOL didFirstLaunch = [[NSUserDefaults standardUserDefaults] boolForKey:@"DidFirstLaunch"];

if (!didFirstLaunch) {

    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"DidFirstLaunch"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, window.frame.size.width, window.frame.size.height)];
    //add some stuff to your transparent view. Example background color
    [v setBackgroundColor:[UIColor orangeColor]];

    // Finally set the alpha value
    [v setAlpha:0.3];
    [window addSubview:v];

}

希望它有所帮助。 ;)

答案 1 :(得分:0)

对于问题的透明覆盖部分......

我认为最简单的方法是创建一个覆盖整个屏幕的子视图,然后减去所需的部分。这里有一些可能有用的Swift代码:

// Create a view filling the screen.
let overlay = UIView(frame: CGRectMake(0, 0, 
    UIScreen.mainScreen().bounds.width,
    UIScreen.mainScreen().bounds.height))

// Set a semi-transparent, black background.
overlay.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.85)

// Create the initial layer from the view bounds.
let maskLayer = CAShapeLayer()
maskLayer.frame = overlay.bounds
maskLayer.fillColor = UIColor.blackColor().CGColor

// Create the frame for the portion that you want to remove. 
// You could get this from a container view that holds all of 
// the subviews that you want to see behind the overlay.
let rect = CGRectMake(50, 50, 100, 100)

// Create the path.
let path = UIBezierPath(rect: overlay.bounds)
maskLayer.fillRule = kCAFillRuleEvenOdd

// Append the rectangle to the path so that it is subtracted.
path.appendPath(UIBezierPath(rect: rect))
maskLayer.path = path.CGPath

// Set the mask of the view.
overlay.layer.mask = maskLayer

// Add the view so it is visible.
self.view.addSubview(overlay)

以上代码的实际效果如下:

enter image description here

我在CocoaPods中添加了library,允许您创建带有矩形/圆形孔的半透明叠加层,允许用户与叠加层后面的视图进行交互。我用它为我们的一个应用程序创建了这个教程:

Tutorial using the TAOverlayView

该库名为TAOverlayView,在Apache 2.0下是开源的。