美文网首页iOS Developer
简单自定义一个枚举类

简单自定义一个枚举类

作者: 静花寒 | 来源:发表于2016-01-20 21:40 被阅读910次

    枚举对于小白来说,都是比较难写的,虽然平时用的比较多,但真正要自己写个枚举,相信大家都会皱起眉头吧,今天我就给大家简单实现下枚举类,整个过程十分简单
    关键代码
    typedef NS_ENUM(,){

    };
    NS_ENUM(,)是系统给我们的一个自定义枚举类,第一个参数就是一个无符号的长整型,第二个参数就是我们定义的枚举类

    typedef NS_ENUM(NSUInteger,XWPushPopPresentDismissTransitionType){
        XWPushPopPresentDismissTransitionTypePush = 0,
        XWPushPopPresentDismissTransitionTypePop,
        XWPushPopPresentDismissTransitionTypePresent,
        XWPushPopPresentDismissTransitionTypeDismiss
    };
    

    这是我参照简友写的一个页面转换枚举
    然后我们声明两个初始化方法

    + (instancetype)transitionWithType:(XWPushPopPresentDismissTransitionType)type;
    
    - (instancetype)initWithTransitionType:(XWPushPopPresentDismissTransitionType)type;
    

    然后在.m中实现下
    首先在延展中设定一个type,方便之后在方法中调用

    @property (nonatomic,assign) XWPushPopPresentDismissTransitionType type;
    
    + (instancetype)transitionWithType:(XWPushPopPresentDismissTransitionType)type
    {
        return [[self alloc] initWithTransitionType:type];
    }
    
    - (instancetype)initWithTransitionType:(XWPushPopPresentDismissTransitionType)type
    {
        self = [super init];
        if (self) {
            _type = type;
        }
        return self;
    }
    

    这样所有准备工作就完成了,之后就是在需要进行方法枚举的地方,用swich方法进行调用

    switch (_type) {
            case XWPushPopPresentDismissTransitionTypePush:
                //方法1
                [self pushAnimation:transitionContext];
                break;
            case XWPushPopPresentDismissTransitionTypePop:
                //方法2
                [self popAnimation:transitionContext];
                break;
            case XWPushPopPresentDismissTransitionTypePresent:
                //方法3
                [self presentAnimation:transitionContext];
                break;
            case XWPushPopPresentDismissTransitionTypeDismiss:
                //方法4
                [self dismissAnimation:transitionContext];
                break;
        }
    

    一个简单的枚举就做完了。

    相关文章

      网友评论

        本文标题:简单自定义一个枚举类

        本文链接:https://www.haomeiwen.com/subject/knslkttx.html