iOS - 自定义确认 View
<p><p>一旦用户按下按钮并完成操作,我正在创建一个自定义控件。我试图在添加专辑时复制苹果音乐应用程序的行为,它在中心显示确认 View ,并带有一个复选标记,如下所示。是否有任何类似的 cocoa 控制可供使用? </p>
<p> <img src="/image/PZjs6.jpg" alt="enter image description here"/> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>(迅速)
创建一个单例类</p>
<p>自定义 View 类:UIView {</p>
<pre><code>class var sharedView : CustomView {
struct Static {
static var instance : CustomView?
static var token : dispatch_once_t = 0
}
dispatch_once(&Static.token) {
Static.instance = CustomView()
}
return Static.instance!
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func showInView(view:UIWindow) {
var image = UIImage(named:"SomeImage")
self.frame = view.frame
var originX = view.center.x
var originY = view.center.y
let centerView = UIImageView()
centerView.center = CGPointMake(originX, originY)
centerView.contentMode = UIViewContentMode.Center
centerView.image = image
centerView.alpha = 0
self.addSubview(centerView)
view.addSubview(self)
UIView.animateWithDuration(1, animations: { () -> Void in
centerView.alpha = 1
}) { (_) -> Void in
UIView.animateWithDuration(1, animations: { () -> Void in
centerView.frame.size = CGSizeMake(0,0)
centerView.alpha = 0
}) { (_) -> Void in
self.hide()
}
}
}
func hide()
{
if self.superview != nil
{
self.removeFromSuperview()
}
}
</code></pre>
<p>}</p>
<p>在您的 viewController 中,您只需调用方法 CustomView.sharedView.showInView(view:UIApplication.sharedApplication.keyWindow())</p>
<blockquote>
<p>Objective c .h</p>
</blockquote>
<pre><code>#import <UIKit/UIKit.h>
@interface CustomView : UIView
+ (instancetype)sharedInstance;
-(void)showInView:(UIWindow*)view;
@end
</code></pre>
<blockquote>
<p>objective c .m</p>
</blockquote>
<pre><code>#import "CustomView.h"
@implementation CustomView
+ (instancetype)sharedInstance
{
static CustomView *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [ init];
});
return sharedInstance;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = ;
if (self) {
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = ;
if (self) {
}
return self;
}
-(void)showInView:(UIWindow*)view {
UIImage *image = ;
self.frame = view.frame;
CGFloat originX = view.center.x;
CGFloat originY = view.center.y;
UIImageView *centerView = ;
centerView.center = CGPointMake(originX, originY);
centerView.contentMode = UIViewContentModeCenter;
centerView.image = image;
centerView.alpha = 0;
;
;
[UIView animateWithDuration:1 animations:^{
centerView.alpha = 1;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1 animations:^{
centerView.frame = CGRectMake(originX, originY, 0, 0);
centerView.alpha = 0;
} completion:^(BOOL finished) {
;
}];
}];
}
-(void)hideView {
if(self.superview) {
;
}
}
@end
</code></pre>
<p>在您的文件中导入 CustomView.h 并</p>
<pre><code>[ showInView:[keyWindow]];
</code></pre></p>
<p style="font-size: 20px;">关于iOS - 自定义确认 View ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31486125/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31486125/
</a>
</p>
页:
[1]