Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace animation name with Enums #22

Closed
PGLongo opened this issue Feb 8, 2015 · 12 comments · Fixed by #85
Closed

Replace animation name with Enums #22

PGLongo opened this issue Feb 8, 2015 · 12 comments · Fixed by #85

Comments

@PGLongo
Copy link

PGLongo commented Feb 8, 2015

I think it should be better use enum for animation name, curve and properties. If you agree, I could take care about it.

@schneiderandre
Copy link
Contributor

Yes, instead of using Strings like "fadeIn" we could use an enum like this

enum Animation: String {
    case FadeIn = "FadeIn"
}

And since all properties are @IBInspectable we have to use rawValue.
This gives us the type safety of an Enum in Code and we are still able to set the properties in Interface Builder.

@jamztang
Copy link
Contributor

jamztang commented Feb 9, 2015

I would like to propose an alternative is to use typealise and constant, to keep the animation in String type value, but clear enough for the public interface know it's not any random string

@PGLongo
Copy link
Author

PGLongo commented Feb 9, 2015

In my opinion if String type is not necessary an enum should be used in this case. Instead of search the animation name/constant I could make Animation. and see all the possibility and in Interface Builder there would be a select box instead of a textfield.

@jamztang explain why you prefer String.

@jamztang
Copy link
Contributor

jamztang commented Feb 9, 2015

I do prefer enum. Main reason we have to keep it as string type is because IBInpectables does not support drop down for Enums. Unless it already does?

@PGLongo
Copy link
Author

PGLongo commented Feb 9, 2015

My mistake. It works only with UIColor.

The only way is with workaround like :

enum Animation: String {
    case FadeIn = "FadeIn"
}

@IBDesignable
class SpringView: UIView {

    var animation : Animation = .FadeIn{
        didSet {
            self.animationName = animation.rawValue
        }
    }

    @IBInspectable var animationName: String = ""

@schneiderandre
Copy link
Contributor

@jamztang Using Enums or String Arrays is not possible for IBInspectables.
I would also try to avoid two variables for setting the same property. This leads to confusion.

I would prefer an Enumeration, like I wrote before. In InterfaceBuilder you have to use the String, and in code you can also use the String or the Enumeration:

springView.animation = SpringView.Animation.FadeIn.rawValue

@PGLongo
Copy link
Author

PGLongo commented Feb 9, 2015

@schneiderandre Enum doesn't appear in InterfaceBuilder.

@schneiderandre
Copy link
Contributor

@PGLongo right, that's what I wrote, but as I mentioned we can use:

@IBInspectable var animation: String

and set it

springView.animation = SpringView.Animation.FadeIn.rawValue

@jamztang
Copy link
Contributor

jamztang commented Feb 9, 2015

Ok so this is to my surprise that it's not working for @IBInspectables :(

typealias AnimationName = String

class SpringView : UIView {
    @IBInspectable var animation : AnimationName = ""
}

I'm actually looking into something like this:

public struct SpringAnimation {
    static let FadeIn = "fadeIn"
    static let FadeOut = "fadeOut"
    static let SlideLeft = "slideLeft"
}

// So to set it will be slightly nicer
springView.animation = SpringAnimation.FadeIn

I do have one for Objective-C but I guess it's way too much addition to the code base, so I'll just link it here for our reference.

Double property from @PGLongo is a clever workaround but if we change the animation to animationName will cause incompatibility issues for v1.0.0 which is already out there.

I'm okay to go with either @schneiderandre solution or the struct solution.

@SachiraChin
Copy link

Swift 1.2 supports @objc enums.

https://developer.apple.com/swift/blog/?id=22

@PGLongo
Copy link
Author

PGLongo commented Mar 23, 2015

@SachiraChin also objc enums doesn't appear in InterfaceBuilder.

@jamztang
Copy link
Contributor

Right. I guess right note the best is to just keep our options open, optimistically I think we might see Apple support enums in the future

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants