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

enhancement - expose primitive values/variables or document methods to allow hooking into variables changing #2

Closed
johndpope opened this issue May 18, 2017 · 9 comments

Comments

@johndpope
Copy link

I used to use this library in objective-c days
https://github.com/danielebogo/PRTween
Perhaps you could cherry pick helpful stuff.

I want to hook into a value that gets updated.
I want to have a ticker style uilabel that rotates when the length is too long.

I'm using snapkit through out project / thus far code looks like this (not full functional)
I want my label's left offset to change over a duration. eg.

UIView.animate(
           withDuration:5.0,
           delay: 0.0,
           options: [UIViewAnimationOptions.curveLinear, UIViewAnimationOptions.beginFromCurrentState, UIViewAnimationOptions.repeat],
           animations: {
              self.labelXoffset = 45 // want a way to hook into a tween value.
               self.label.snp.updateConstraints { (make) -> Void in
                   make.left.equalTo(self.labelXoffset)
               }

       },
           completion: { _ in
               
       })

https://gist.github.com/johndpope/5eef5fab18ad20b467b8da81032fa83f

@johndpope
Copy link
Author

(it maybe a case of simply documenting this functional as I dive deeper into classes looking for how to.) perhaps I could extend from interpolation.swift

@johndpope
Copy link
Author

perhaps looking at this library sample code - what's not clear to me is hooking into anything but the cgrect.. I want to interpolate a cgsize or variable.

    let newWidth = CGFloat(200)
    let newHeight = CGFloat(100)
    let newFrame = CGRect(x: self.view.center.x - newWidth/2,
                          y: self.view.center.y - newHeight/2,
                          width: newWidth,
                          height: newHeight)
    
    // Create an interpolation action
    // We can use a closure for the from argument to get the current frame of the view
    // Note the use of [unowned self] closures to break retain cycle, as scheduler has the same lifetime as self
    let action = InterpolationAction(from: { [unowned self] in self.squareView.frame },
                                     to: newFrame,
                                     duration: 2,
                                     easing: .exponentialInOut,
                                     update: { [unowned self] in self.squareView.frame = $0 })       

@johndpope
Copy link
Author

johndpope commented May 18, 2017

this function - actually looks perfect / would be nice to expose more documentation in readme as per PRTween.

  public init(from startValue: T,
                to endValue: T,
                duration: Double,
                easing: Easing,
                update: @escaping (_: T) -> ()) {
        
        self.duration = duration
        self.updateHandler = update
        self.easing = easing
        
        self.startTweenableValue = .constant(startValue)
        self.endTweenableValue = .constant(endValue)
    }

@SteveBarnegren
Copy link
Owner

Hey @johndpope

You can use that exact same code, but supply CGFloats or CGSize instead of CGRects if you like, as those classes take generic arguments.

Here is an example from a project that I'm working on:

 let fade = InterpolationAction(from: CGFloat(0),
                                       to: CGFloat(1),
                                       duration: duration,
                                       easing: .exponentialOut) {
                                        [unowned self] in
                                        self.canLogo.alpha = $0
        }

Any type that conforms to the Tweenable protocol can be used. Out of the box, CGSize and CGFloat both conform

Does that help?

@johndpope
Copy link
Author

screen shot 2017-05-18 at 10 45 38 am

@johndpope
Copy link
Author

thanks Steve.

@johndpope
Copy link
Author

still think it would go a long way to have this in readme

@SteveBarnegren
Copy link
Owner

Glad you got it working!

I'll put it on my todo list to make the readme a bit clearer. I'll take a look at PRTween too, to see how that project is documented

@johndpope
Copy link
Author

this is working mint.


    func updateUpdateConstraint(offset:CGFloat){
        self.label.snp.updateConstraints { (make) -> Void in
            make.left.equalTo(offset)
        }
    }
    
    func startRotating() {

      
        let fade = InterpolationAction(from: CGFloat(0),
                                       to: CGFloat(100),
                                       duration: 20,
                                       easing: .exponentialOut) {
                                        [unowned self] in
                                        self.updateUpdateConstraint(offset: $0)
        }
        scheduler.run(action: fade)
    }


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

No branches or pull requests

2 participants