OStack程序员社区-中国程序员成长平台

标题: iOS - 自定义确认 View [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:05
标题: iOS - 自定义确认 View

一旦用户按下按钮并完成操作,我正在创建一个自定义控件。我试图在添加专辑时复制苹果音乐应用程序的行为,它在中心显示确认 View ,并带有一个复选标记,如下所示。是否有任何类似的 cocoa 控制可供使用?

enter image description here



Best Answer-推荐答案


(迅速) 创建一个单例类

自定义 View 类:UIView {

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()
    }
}

}

在您的 viewController 中,您只需调用方法 CustomView.sharedView.showInView(view:UIApplication.sharedApplication.keyWindow())

Objective c .h

#import <UIKit/UIKit.h>

@interface CustomView : UIView

+ (instancetype)sharedInstance;
-(void)showInViewUIWindow*)view;

@end

objective c .m

#import "CustomView.h"

@implementation CustomView

+ (instancetype)sharedInstance
{
    static CustomView *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[CustomView alloc] init];
    });
    return sharedInstance;
}

- (instancetype)initWithCoderNSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {

    }
    return self;
}

- (instancetype)initWithFrameCGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}


-(void)showInViewUIWindow*)view {

    UIImage *image = [UIImage imageNamed"img.png"];
    self.frame = view.frame;
    CGFloat originX = view.center.x;
    CGFloat originY = view.center.y;

    UIImageView *centerView = [UIImageView new];
    centerView.center = CGPointMake(originX, originY);
    centerView.contentMode = UIViewContentModeCenter;
    centerView.image = image;
    centerView.alpha = 0;
    [self addSubview:centerView];
    [view addSubview:self];

    [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) {

            [self hideView];

        }];

    }];
}

-(void)hideView {
    if(self.superview) {
        [self removeFromSuperview];
    }
}


@end

在您的文件中导入 CustomView.h 并

[[CustomView sharedInstance] showInView:[[UIApplication sharedApplication]keyWindow]];

关于iOS - 自定义确认 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31486125/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4