Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 4 Expression type '@value CGRect' is ambiguous without more context

Tags:

swift

cgrect

I am trying to implement a animation for a rectangle to perform modal vertical swipe.However, when I try to compile the code I get the following error "Swift 4 Expression type '@value CGRect' is ambiguous without more context". I have isolated the issue to the arguments that is being passed to the CGRect init value, but according the Apple's iOS documentation these parameters should be enough to specify the 'frame' view that I need to animate.

Here is my code:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    guard
        let fromView = transitionContext.viewController(forKey: .from),
        let toView = transitionContext.viewController(forKey: .to)

    else {
            return
    }
    let containerView = transitionContext.containerView
    containerView.insertSubview((toView.view)!, belowSubview: (fromView.view)!)

    let toFrame = transitionContext.finalFrame(for: toView)

    let screenBounds = UIScreen.main.bounds

    let bottomLeftCorner = CGPoint(x: 0, y: screenBounds.height)

    let finalFrameForVC = CGRect(origin: bottomLeftCorner, size: screenBounds.size)

    UIView.animate(withDuration: 2, delay: 1,
                   options: [], (using: transitionContext.finalFrame(for: fromView)),
        animations: {
            fromView.view.frame = finalFrameForVC
    },
        completion: { _ in
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
    })
}
like image 739
CoderQueue Avatar asked Jul 02 '18 16:07

CoderQueue


2 Answers

I had the same error when I tried to multiply view.frame.height by a Double. If you are doing any math on screenBounds properties, make sure the types match.

like image 149
iainhunter Avatar answered Nov 12 '22 02:11

iainhunter


My problem was that I was trying to perform an operation between a CGFloat variable and Double variable.

Fix: Convert the Double variable to CGFloat. Check your variable types.

I hope it helps! :)

like image 27
valbu17 Avatar answered Nov 12 '22 04:11

valbu17