2017-06-15 30 views
0

我按照此順序顯示圖像滑塊。來自框架的UIViewController

的UIViewController>的UITableView>的UITableViewCell> UICollectionview> UICollectionViewCell>的UIImage

用戶可以滑動UICollectionview和視圖images.Problem是,我需要做的動畫。當用戶點擊我的UICollectionviewCell時,它應該從該單元格開始動畫並顯示全屏,就像在這個庫中一樣。

https://github.com/suzuki-0000/SKPhotoBrowser

問題是,我需要使用MWPhotoBrowser,我不能提出這樣的。

我在想使用英雄動畫庫。

https://github.com/lkzhao/Hero

不過我的看法層次結構和它們的例子是不同的。我該怎麼辦?

回答

1

你必須使用自定義的過渡

參考:

https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/CustomizingtheTransitionAnimations.html

郵政編碼工作

在圖像選擇

加入VC_A

var selectedImage: UIImageView? 
let transition = PopAnimator() 

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 
     super.viewWillTransition(to: size, with: coordinator) 

     coordinator.animate(
      alongsideTransition: {context in 
      self.bgImage.alpha = (size.width>size.height) ? 0.25 : 0.55 
      self.positionListItems() 
      }, 
      completion: nil 
     ) 
     } 
//position all images inside the list 
    func positionListItems() { 
    let listHeight = listView.frame.height 
    let itemHeight: CGFloat = listHeight * 1.33 
    let aspectRatio = UIScreen.main.bounds.height/UIScreen.main.bounds.width 
    let itemWidth: CGFloat = itemHeight/aspectRatio 

    let horizontalPadding: CGFloat = 10.0 

    for i in herbs.indices { 
     let imageView = listView.viewWithTag(i) as! UIImageView 
     imageView.frame = CGRect(
     x: CGFloat(i) * itemWidth + CGFloat(i+1) * horizontalPadding, y: 0.0, 
     width: itemWidth, height: itemHeight) 
    } 

    listView.contentSize = CGSize(
     width: CGFloat(herbs.count) * (itemWidth + horizontalPadding) + horizontalPadding, 
     height: 0) 
    } 

// On image selection 
VC_B.transitioningDelegate = self 
    present(VC_B, animated: true, completion: nil) 



    // add extension 
extension VC_A: UIViewControllerTransitioningDelegate { 

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
    transition.originFrame = selectedImage!.superview!.convert(selectedImage!.frame, to: nil) 

    transition.presenting = true 
    selectedImage!.isHidden = true 

    return transition 
    } 

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
    transition.presenting = false 
    return transition 
    } 
} 

和動畫類

class PopAnimator: NSObject, UIViewControllerAnimatedTransitioning { 

    let duration = 1.0 
    var presenting = true 
    var originFrame = CGRect.zero 

    var dismissCompletion: (()->Void)? 

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { 
    return duration 
    } 

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { 
    let containerView = transitionContext.containerView 

    let toView = transitionContext.view(forKey: .to)! 

    let herbView = presenting ? toView : transitionContext.view(forKey: .from)! 

    let initialFrame = presenting ? originFrame : herbView.frame 
    let finalFrame = presenting ? herbView.frame : originFrame 

    let xScaleFactor = presenting ? 

     initialFrame.width/finalFrame.width : 
     finalFrame.width/initialFrame.width 

    let yScaleFactor = presenting ? 

     initialFrame.height/finalFrame.height : 
     finalFrame.height/initialFrame.height 

    let scaleTransform = CGAffineTransform(scaleX: xScaleFactor, y: yScaleFactor) 

    if presenting { 
     herbView.transform = scaleTransform 
     herbView.center = CGPoint(
     x: initialFrame.midX, 
     y: initialFrame.midY) 
     herbView.clipsToBounds = true 
    } 

    containerView.addSubview(toView) 
    containerView.bringSubview(toFront: herbView) 

    UIView.animate(withDuration: duration, delay:0.0, usingSpringWithDamping: 0.4, 
     initialSpringVelocity: 0.0, 
     animations: { 
     herbView.transform = self.presenting ? 
      CGAffineTransform.identity : scaleTransform 
     herbView.center = CGPoint(x: finalFrame.midX, 
            y: finalFrame.midY) 
     }, 
     completion:{_ in 
     if !self.presenting { 
      self.dismissCompletion?() 
     } 
     transitionContext.completeTransition(true) 
     } 
    ) 
    } 

} 
+0

非常感謝你的幫助。我嘗試閱讀,我不知道如何修改到我的視圖層次結構。 如果可能,請在我的演示中請提供建議?我爲此創建了一個小型項目。它需要使用MWPhotoBrowser,以便它可以支持視頻。 https://github.com/khantthulinn/photo_tap –

相關問題