LoginSignup
9
8

More than 5 years have passed since last update.

Swift から MDCSwipeToChoose を使う

Last updated at Posted at 2014-11-14

MDCSwipeToChoose を Swift から使ってみる時のメモ。

Podfile の準備

Podfile を作って pod install

platform :ios, "8.0"
pod 'MDCSwipeToChoose'

Bridging Header の準備

UIKit も import しておかないとエラーになります。

BridgingHeader.h
#ifndef BridgingHeader_h
#define BridgingHeader_h

#import <UIKit/UIKit.h>
#import <MDCSwipeToChoose/MDCSwipeToChoose.h>

#endif

ViewController 中に配置

import UIKit

// ... in a view controller

override func viewDidLoad() {
    super.viewDidLoad()

    var options = MDCSwipeToChooseViewOptions()
    options.delegate = self
    options.likedText = "Keep"
    options.likedColor = UIColor.blueColor()
    options.nopeText = "Delete"
    options.onPan = { state -> Void in
        if state.thresholdRatio == 1 && state.direction == MDCSwipeDirection.Left {
            println("Photo deleted!")
        }
    }

    var view = MDCSwipeToChooseView(frame: self.view.bounds, options: options)
    view.imageView.image = UIImage(named: "photo.png")
    self.view.addSubview(view)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// This is called when a user didn't fully swipe left or right.
func viewDidCancelSwipe(view: UIView) -> Void{
    println("Couldn't decide, huh?")
}

// Sent before a choice is made. Cancel the choice by returning `false`. Otherwise return `true`.
func view(view: UIView, shouldBeChosenWithDirection: MDCSwipeDirection) -> Bool{
    if (shouldBeChosenWithDirection == MDCSwipeDirection.Left) {
        return true;
    } else {
        // Snap the view back and cancel the choice.
        UIView.animateWithDuration(0.16, animations: { () -> Void in
            view.transform = CGAffineTransformIdentity
            view.center = view.superview!.center
        })
        return false;
    }
}

// This is called then a user swipes the view fully left or right.
func view(view: UIView, wasChosenWithDirection: MDCSwipeDirection) -> Void{
    if wasChosenWithDirection == MDCSwipeDirection.Left {
        println("Photo deleted!")
    }else{
        println("Photo saved!")
    }
}

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8