0

I believe I have the code for determining if my application has been opened before set in stone:

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

    //Code for assembling overlay view
}

I put this code in the didFinishLaunchingWithOptions method.

I want the transparent overlay view to only cover 3/4 of the iPhone screen and be slightly transparent so you can still see a little of the main page of my application -- it will have a couple pictures and text on it (basically a simple tutorial). How do I go about creating a simple view of this nature?

4

2 回答 2

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];

}

希望能帮助到你。;)

于 2012-07-30T23:57:06.073 回答
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)

以下是上述代码的实际效果:

在此处输入图像描述

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

使用 TAOverlayView 的教程

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

于 2016-03-15T15:06:03.110 回答