如何实现SwipeView?

时间:2015-12-11 14:32:42

标签: ios swift

我想滑动每个图片以切换到另一个图片,如图库应用。我现在正在使用此https://www.googleapis.com/auth/userinfo.email,但我不知道如何实现它。我应该在PhotoDetailViewController中拖动一个集合视图,还是只在编码中使用它。愿任何人帮助我。

这是我的代码:

import Foundation
import UIKit
import AAShareBubbles
import SwipeView

class PhotoDetailViewController: UIViewController, AAShareBubblesDelegate, SwipeViewDataSource, SwipeViewDelegate {


  @IBOutlet var topView: UIView!
  @IBOutlet var bottomView: UIView!
  @IBOutlet var photoImageView: UIImageView!
  var photoImage = UIImage()
  var checkTapGestureRecognize = true
  var swipeView: SwipeView = SwipeView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height))

  override func viewDidLoad() {

    title = "Photo Detail"
    super.viewDidLoad()
    photoImageView.image = photoImage
    swipeView.dataSource = self
    swipeView.delegate = self
    let swipe = UISwipeGestureRecognizer(target: self, action: "swipeMethod")
    swipeView.addGestureRecognizer(swipe)
    swipeView.addSubview(photoImageView)
    swipeView.pagingEnabled = false
    swipeView.wrapEnabled = true

  }

  func swipeView(swipeView: SwipeView!, viewForItemAtIndex index: Int, reusingView view: UIView!) -> UIView! {
    return photoImageView
  }

  func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
  }

  func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return images.count
  }

  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("SwipeCell", forIndexPath: indexPath) as! SwipeViewPhotoCell
    return cell
  }

  @IBAction func onBackClicked(sender: AnyObject) {
    self.navigationController?.popViewControllerAnimated(true)
  }


  @IBAction func onTabGestureRecognize(sender: UITapGestureRecognizer) {
    print("on tap")
    if checkTapGestureRecognize == true {
      bottomView.hidden = true
      topView.hidden = true
      self.navigationController?.navigationBarHidden = true
      let screenSize: CGRect = UIScreen.mainScreen().bounds
      let screenWidth = screenSize.width
      let screenHeight = screenSize.height
      photoImageView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
      checkTapGestureRecognize = false
      showAminationOnAdvert()
    }
    else if checkTapGestureRecognize == false {
      bottomView.hidden = false
      topView.hidden = false
      self.navigationController?.navigationBarHidden = false
      checkTapGestureRecognize = true
    }
  }

  func showAminationOnAdvert() {
    let transitionAnimation = CATransition();
    transitionAnimation.type = kCAEmitterBehaviorValueOverLife
    transitionAnimation.subtype = kCAEmitterBehaviorValueOverLife
    transitionAnimation.duration = 2.5
    transitionAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transitionAnimation.fillMode = kCAFillModeBoth
    photoImageView.layer.addAnimation(transitionAnimation, forKey: "fadeAnimation")

  }


  @IBAction func onShareTouched(sender: AnyObject) {

    print("share")

    let myShare = "I am feeling *** today"

    let shareVC: UIActivityViewController = UIActivityViewController(activityItems: [myShare], applicationActivities: nil)
    self.presentViewController(shareVC, animated: true, completion: nil)
    //    print("share bubles")
    //    let shareBubles: AAShareBubbles = AAShareBubbles.init(centeredInWindowWithRadius: 100)
    //    shareBubles.delegate = self
    //    shareBubles.bubbleRadius = 40
    //    shareBubles.sizeToFit()
    //    //shareBubles.showFacebookBubble = true
    //    shareBubles.showTwitterBubble = true
    //    shareBubles.addCustomButtonWithIcon(UIImage(named: "twitter"), backgroundColor: UIColor.whiteColor(), andButtonId: 100)
    //    shareBubles.show()

  }

  @IBAction func playAutomaticPhotoImages(sender: AnyObject) {
    animateImages(0)
  }

  func animateImages(no: Int) {
    var number: Int = no
    if number == images.count - 1 {
      number = 0
    }
    let name: String = images[number]
    self.photoImageView!.alpha = 0.5
    self.photoImageView!.image = UIImage(named: name)

    //code to animate bg with delay 2 and after completion it recursively calling animateImage method
    UIView.animateWithDuration(2.0, delay: 0.8, options:UIViewAnimationOptions.CurveEaseInOut, animations: {() in
      self.photoImageView!.alpha = 1.0;
      },
      completion: {(Bool) in
        number++;
        self.animateImages(number);
        print(String(images[number]))
    })
  }
}

1 个答案:

答案 0 :(得分:1)

只需将UIView拖放到storyboard / XIB,然后将其自定义类设置为SwipeView。

还将委托和数据源设置为视图控制器,其中包含您刚刚拖动的UIView。

然后在viewcontroller中,实现所需的委托方法,类似于为tableview实现方法的方法。