2

I am working on creating a custom control once user presses a button and action completes. I'm trying to replicate behavior of apple music app when album is added it shows confirmation view in center with a check mark as shown below. Are there any similar cocoa controls available to use?

enter image description here

Daniyar
  • 2,975
  • 2
  • 26
  • 39
Obj-Swift
  • 2,802
  • 3
  • 29
  • 50

1 Answers1

1

(swift) Create a singleton class

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

}

In your viewController you can just call the method CustomView.sharedView.showInView(view:UIApplication.sharedApplication.keyWindow())

Objective c .h

#import <UIKit/UIKit.h>

@interface CustomView : UIView

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

Import CustomView.h in your file and

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