Toast 消息使用 Swift
Toast message using Swift
我想知道是否可以重现当您在 Apple Music 中将专辑添加到您的 collection 时 Apple 使用的吐司消息(好吧,事实上我真的不知道如何命名这个灰色屏幕中间带有勾号的框,但 'toast' 似乎适合)。关于如何使用 Swift?
来实现的任何想法
这是 Obj-C 而不是 Swift 但我有一个旧项目,里面有类似的东西。 UI 不一样,但我确信更改为您想要的东西很容易。它在深色背景上添加了一个活动指示器(微调器)而不是一个勾号。您可以从任何地方调用此代码,它会在将它添加到主 window 时显示。如果您只在一个地方需要它,那么只需将它添加到呈现视图即可。
想显示时调用此代码:
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 100)];
myView.backgroundColor = [UIColor blackColor];
myView.alpha = 0.8;
myView.layer.cornerRadius = 8;
UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[myView addSubview:act];
act.center = CGPointMake(CGRectGetWidth(myView.frame)/2.0, CGRectGetHeight(myView.frame)/2.0);
[act startAnimating];
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
[mainWindow addSubview:myView];
myView.center = mainWindow.center;
@Gordonium 在 Swift class
中的回答
public class LoaderOverlay {
private var loaderOverlayView: UIView!
class var shared: LoaderOverlay {
struct Static {
static let instance: LoaderOverlay = LoaderOverlay()
}
return Static.instance
}
private func setup() {
loaderOverlayView = UIView(frame: CGRectMake(0, 0, 110, 100))
loaderOverlayView.backgroundColor = UIColor.blackColor()
loaderOverlayView.alpha = 0.8
loaderOverlayView.layer.cornerRadius = 8
let indicator: UIActivityIndicatorView = UIActivityIndicatorView.init(activityIndicatorStyle: .White)
indicator.tag = 1
loaderOverlayView.addSubview(indicator)
indicator.center = CGPointMake(CGRectGetWidth(loaderOverlayView.frame)/2.0, CGRectGetHeight(loaderOverlayView.frame)/2.0)
indicator.startAnimating()
}
// MARK:- Public
public func show() {
if loaderOverlayView == nil {
self.setup()
} else {
let indicator = loaderOverlayView.viewWithTag(1) as! UIActivityIndicatorView
indicator.startAnimating()
}
if let window = UIApplication.sharedApplication().delegate?.window {
window!.addSubview(loaderOverlayView)
loaderOverlayView.center = window!.center
}
}
public func hide() {
let indicator = loaderOverlayView.viewWithTag(1) as! UIActivityIndicatorView
indicator.stopAnimating()
loaderOverlayView.removeFromSuperview()
}
}
用法:
显示
LoaderOverlay.shared.show()
隐藏
LoaderOverlay.shared.hide()
我想知道是否可以重现当您在 Apple Music 中将专辑添加到您的 collection 时 Apple 使用的吐司消息(好吧,事实上我真的不知道如何命名这个灰色屏幕中间带有勾号的框,但 'toast' 似乎适合)。关于如何使用 Swift?
来实现的任何想法这是 Obj-C 而不是 Swift 但我有一个旧项目,里面有类似的东西。 UI 不一样,但我确信更改为您想要的东西很容易。它在深色背景上添加了一个活动指示器(微调器)而不是一个勾号。您可以从任何地方调用此代码,它会在将它添加到主 window 时显示。如果您只在一个地方需要它,那么只需将它添加到呈现视图即可。
想显示时调用此代码:
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 100)];
myView.backgroundColor = [UIColor blackColor];
myView.alpha = 0.8;
myView.layer.cornerRadius = 8;
UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[myView addSubview:act];
act.center = CGPointMake(CGRectGetWidth(myView.frame)/2.0, CGRectGetHeight(myView.frame)/2.0);
[act startAnimating];
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
[mainWindow addSubview:myView];
myView.center = mainWindow.center;
@Gordonium 在 Swift class
中的回答public class LoaderOverlay {
private var loaderOverlayView: UIView!
class var shared: LoaderOverlay {
struct Static {
static let instance: LoaderOverlay = LoaderOverlay()
}
return Static.instance
}
private func setup() {
loaderOverlayView = UIView(frame: CGRectMake(0, 0, 110, 100))
loaderOverlayView.backgroundColor = UIColor.blackColor()
loaderOverlayView.alpha = 0.8
loaderOverlayView.layer.cornerRadius = 8
let indicator: UIActivityIndicatorView = UIActivityIndicatorView.init(activityIndicatorStyle: .White)
indicator.tag = 1
loaderOverlayView.addSubview(indicator)
indicator.center = CGPointMake(CGRectGetWidth(loaderOverlayView.frame)/2.0, CGRectGetHeight(loaderOverlayView.frame)/2.0)
indicator.startAnimating()
}
// MARK:- Public
public func show() {
if loaderOverlayView == nil {
self.setup()
} else {
let indicator = loaderOverlayView.viewWithTag(1) as! UIActivityIndicatorView
indicator.startAnimating()
}
if let window = UIApplication.sharedApplication().delegate?.window {
window!.addSubview(loaderOverlayView)
loaderOverlayView.center = window!.center
}
}
public func hide() {
let indicator = loaderOverlayView.viewWithTag(1) as! UIActivityIndicatorView
indicator.stopAnimating()
loaderOverlayView.removeFromSuperview()
}
}
用法: 显示
LoaderOverlay.shared.show()
隐藏
LoaderOverlay.shared.hide()