【问题标题】:iOS - Custom Confirmation viewiOS - 自定义确认视图
【发布时间】:2015-07-17 23:24:10
【问题描述】:

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

【问题讨论】:

标签: ios objective-c cocoa-touch uikit


【解决方案1】:

(迅速) 创建一个单例类

自定义视图类: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]];

【讨论】:

  • 谢谢。这里面有 Objective-C 版本吗?
  • 我可以为您编写,但您仍然可以导入此文件并包含在您的桥接头中并使用它。
  • 我尝试了这里提到的所有内容:stackoverflow.com/questions/24206732/… 但由于某种原因,我无法将 swift 类添加到我的项目中。如果您可以发布 obj-c 版本,将不胜感激。谢谢。
  • 我已经在我的答案中添加了客观的 c 代码,如果你不能做到,请告诉我:)
  • 我刚刚测试过,它对我有用...你的图片命名正确吗?
猜你喜欢
  • 2021-01-12
  • 2016-08-11
  • 1970-01-01
  • 2016-11-25
  • 2014-08-07
  • 1970-01-01
  • 1970-01-01
  • 2013-08-05
  • 2018-04-03
相关资源
最近更新 更多