iOS - 自定义确认视图

时间:2015-07-17 23:24:10

标签: ios objective-c cocoa-touch uikit

一旦用户按下按钮并完成操作,我正在创建自定义控件。我在添加相册时尝试复制苹果音乐应用的行为,它会在中心显示确认视图,并带有复选标记,如下所示。有没有类似的可可控制可供使用?

enter image description here

1 个答案:

答案 0 :(得分:1)

(SWIFT) 创建一个单例类

class CustomView: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())

  

目标c .h

#import <UIKit/UIKit.h>

@interface CustomView : UIView

+ (instancetype)sharedInstance;
-(void)showInView:(UIWindow*)view;

@end
  

目标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)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {

    }
    return self;
}

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

    }
    return self;
}


-(void)showInView:(UIWindow*)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]];