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?
Asked
Active
Viewed 401 times
2
-
This is a good one: https://github.com/TransitApp/SVProgressHUD – rocky Jul 17 '15 at 23:25
-
1Nice one to achieve the same as Apple's one: https://github.com/pkluz/PKHUD – tbaranes Jul 17 '15 at 23:26
-
Thanks but this is for ongoing activity. I am looking for the one which is shown once the activity is completed. – Obj-Swift Jul 17 '15 at 23:27
-
@Ckouta Exactly what I needed. Thanks. – Obj-Swift Jul 17 '15 at 23:31
1 Answers
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]];

Aditya Koukuntla
- 325
- 3
- 9
-
-
I can write it for you,but you can still import this file and include in your bridging header and use it. – Aditya Koukuntla Jul 20 '15 at 16:09
-
I tried everything mentioned here: http://stackoverflow.com/questions/24206732/cant-use-swift-classes-inside-objective-c but for some reason Im unable to add swift class to my project. Would appreciate if you can post obj-c version. thanks. – Obj-Swift Jul 20 '15 at 16:44
-
I have added objective c code to my answer,let me know if you are not able to do it :) – Aditya Koukuntla Jul 20 '15 at 18:41
-
I just tested,it worked for me...Did you name your image correctly? – Aditya Koukuntla Jul 20 '15 at 18:55
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83790/discussion-between-zhr-and-aditya-koukuntla). – Obj-Swift Jul 20 '15 at 18:56