-4

I have a layer that moves up when I swipe up and then the same layer moves down when I swipe down.

When I swipe up, I don’t want the user to be able to swipe down to activate that animation until the swipe up animation is complete and vice-versa.

How do I accomplish this? I’ve tried disabling the swipe gesture using “isEnabled” but no dice. Some other similar questions have answers that are from very long ago and the syntax is very different. I’m using the latest version of xcode.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
TNasty
  • 105
  • 7
  • Can you share the relevant code? – Malik Mar 05 '19 at 05:47
  • func downWaves() { // Function that moves the waves let moveDown = CABasicAnimation(keyPath: "position.y") moveDown.fromValue = 550 moveDown.toValue = 843 moveDown.duration = 1 moveDown.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear) moveDown.repeatCount = 1 moveDown.fillMode = CAMediaTimingFillMode.forwards moveDown.isRemovedOnCompletion = false hundred.layer.add(moveDown, forKey: "position") – TNasty Mar 05 '19 at 13:42
  • *hundred is the image that I'm moving – TNasty Mar 05 '19 at 13:46
  • Look at this answer https://stackoverflow.com/a/16337973/7842542 . It shows how to stop an animation. Maybe you can use a gesture recogniser to stop an animation and start a new one – Malik Mar 06 '19 at 07:20
  • Thanks Malik, I actually found another approach using the presentation() prosperity. myImage.layer.presentation()?.position.y -> this allowed me to save the y position of my image at anytime such that when I swipe in any direction, the image moves starting from the last place it was found at – TNasty Mar 08 '19 at 03:41

1 Answers1

0

This issue could be solved by adding boolean variable, IF Statement along with a DispatchQueue function to prevent another animation from occurring until the current animation has finished.

import UIKit
var animation_active = false
let animation_duration = 2 //For easy maintenance

func swipe_down() {
    if animation_active == false {
        animation_active == true
        //Animation code goes here. Variable 'animation_active' should be used when performing the animation for easy matinence
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(animation_duration), execute: {
            animation_active = false
        })
    }
}
 

func swipe_up() { //This is exactly the same as swipe_up. Yet it will prevent the swipe animation from occuring when the swipe down animation is occuruing thanks to the variable 'animation_active'
    if animation_active == false {
        animation_active == true
        //Animation code goes here. Variable 'animation_active' should be used when performing the animation for easy matinence
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(animation_duration), execute: {
            animation_active = false
        })
    }
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Matt L
  • 1
  • This works but the only thing is, I want the user to to be able to swipe up or down seamlessly but when I use the animation_active conditions, there is a pause before the animation begins. The other choice I can think of is allowing the user to swipe up or down whenever they want but that means I have to store the last y-coordinate of the image such that when the user does swipe, the animation doesn't reset all the way to the top or the bottom. – TNasty Mar 05 '19 at 13:45