{"id":2531,"url":"https://github.com/modocache/MDCSwipeToChoose","last_synced_at":"2025-08-03T00:32:00.228Z","repository":{"id":15786376,"uuid":"18525562","full_name":"modocache/MDCSwipeToChoose","owner":"modocache","description":"Swipe to \"like\" or \"dislike\" any view, just like Tinder.app. Build a flashcard app, a photo viewer, and more, in minutes, not hours!","archived":false,"fork":false,"pushed_at":"2018-01-29T09:02:14.000Z","size":1494,"stargazers_count":2551,"open_issues_count":50,"forks_count":421,"subscribers_count":75,"default_branch":"master","last_synced_at":"2024-11-28T22:03:25.706Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/modocache.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-04-07T17:06:44.000Z","updated_at":"2024-11-19T21:57:06.000Z","dependencies_parsed_at":"2022-07-16T07:16:12.924Z","dependency_job_id":null,"html_url":"https://github.com/modocache/MDCSwipeToChoose","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modocache%2FMDCSwipeToChoose","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modocache%2FMDCSwipeToChoose/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modocache%2FMDCSwipeToChoose/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modocache%2FMDCSwipeToChoose/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/modocache","download_url":"https://codeload.github.com/modocache/MDCSwipeToChoose/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228383695,"owners_count":17911329,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-01-05T20:16:16.077Z","updated_at":"2024-12-06T18:30:33.384Z","avatar_url":"https://github.com/modocache.png","language":"Objective-C","funding_links":[],"categories":["UI","Objective-C","Objective-C  Stars 1000以内排名整理"],"sub_categories":["Cards","Other free courses"],"readme":"# MDCSwipeToChoose\n\n[![Build Status](https://travis-ci.org/modocache/MDCSwipeToChoose.svg?branch=master)](https://travis-ci.org/modocache/MDCSwipeToChoose)\n\nSwipe to \"like\" or \"dislike\" any view, just like Tinder.app. Build a flashcard app, a photo viewer, and more, in minutes, not hours!\n\n- Use `UIView+MDCSwipeToChoose` to add a swipe gesture and callbacks to any `UIView`.\n- Use `MDCSwipeToChooseView` to get a UI nearly identical to Tinder.app in just a few lines of code.\n\n![](http://cl.ly/image/0M1j1J0E0s3G/MDCSwipeToChoose-v0.2.0.gif)\n\nYou may view slides on some the architecture decisions that went into this library [here](http://modocache.io/ios-ui-component-api-design).\n\n## How to Install via CocoaPods\n\nPlace the following in your Podfile and run `pod install`:\n\n```objc\npod \"MDCSwipeToChoose\"\n```\n\n## How to Use\n\nCheck out [the sample app](https://github.com/modocache/MDCSwipeToChoose/tree/master/Examples/LikedOrNope) for an example of how to use `MDCSwipeToChooseView` to build the UI in the GIF above.\n\n\u003e **NOTE:** You must run `pod install` in the `Examples/LikedOrNope` directory before building the example app.\n\nEvery public class contains documentation in its header file.\n\n### Swiping Yes/No\n\nThe following is an example of how you can use `MDCSwipeToChooseView` to display a photo. The user can choose to delete it by swiping left, or save it by swiping right.\n\n#### Objective-C\n\n```objc\n#import \u003cMDCSwipeToChoose/MDCSwipeToChoose.h\u003e\n\n// ... in a view controller\n\n#pragma mark - Creating and Customizing a MDCSwipeToChooseView\n\n- (void)viewDidLoad {\n    [super viewDidLoad];\n\n    // You can customize MDCSwipeToChooseView using MDCSwipeToChooseViewOptions.\n    MDCSwipeToChooseViewOptions *options = [MDCSwipeToChooseViewOptions new];\n    options.likedText = @\"Keep\";\n    options.likedColor = [UIColor blueColor];\n    options.nopeText = @\"Delete\";\n    options.onPan = ^(MDCPanState *state){\n        if (state.thresholdRatio == 1.f \u0026\u0026 state.direction == MDCSwipeDirectionLeft) {\n            NSLog(@\"Let go now to delete the photo!\");\n        }\n    };\n\n    MDCSwipeToChooseView *view = [[MDCSwipeToChooseView alloc] initWithFrame:self.view.bounds\n                                                                     options:options];\n    view.imageView.image = [UIImage imageNamed:@\"photo\"];\n    [self.view addSubview:view];\n}\n\n#pragma mark - MDCSwipeToChooseDelegate Callbacks\n\n// This is called when a user didn't fully swipe left or right.\n- (void)viewDidCancelSwipe:(UIView *)view {\n    NSLog(@\"Couldn't decide, huh?\");\n}\n\n// Sent before a choice is made. Cancel the choice by returning `NO`. Otherwise return `YES`.\n- (BOOL)view:(UIView *)view shouldBeChosenWithDirection:(MDCSwipeDirection)direction {\n    if (direction == MDCSwipeDirectionLeft) {\n        return YES;\n    } else {\n        // Snap the view back and cancel the choice.\n        [UIView animateWithDuration:0.16 animations:^{\n            view.transform = CGAffineTransformIdentity;\n            view.center = [view superview].center;\n        }];\n        return NO;\n    }\n}\n\n// This is called then a user swipes the view fully left or right.\n- (void)view:(UIView *)view wasChosenWithDirection:(MDCSwipeDirection)direction {\n    if (direction == MDCSwipeDirectionLeft) {\n        NSLog(@\"Photo deleted!\");\n    } else {\n        NSLog(@\"Photo saved!\");\n    }\n}\n```\n\n#### Swift\n\nTo use objective-c code from swift, you need to use bridging-header.\n\n```BridgingHeader\n#ifndef BridgingHeader_h\n#define BridgingHeader_h\n\n#import \u003cUIKit/UIKit.h\u003e\n#import \u003cMDCSwipeToChoose/MDCSwipeToChoose.h\u003e\n\n#endif\n\n```\n\n```swift\nimport UIKit\n\nclass ViewController: UIViewController {\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n\t\tlet options = MDCSwipeToChooseViewOptions()\n\t\toptions.delegate = self\n\t\toptions.likedText = \"Keep\"\n\t\toptions.likedColor = UIColor.blue\n\t\toptions.nopeText = \"Delete\"\n\t\toptions.nopeColor = UIColor.red\n\t\toptions.onPan = { state -\u003e Void in\n\t\t\tif state?.thresholdRatio == 1 \u0026\u0026 state?.direction == .left {\n\t\t\t\tprint(\"Photo deleted!\")\n\t\t\t}\n\t\t}\n\n\t\tlet view = MDCSwipeToChooseView(frame: self.view.bounds, options: options)\n\t\tview?.imageView.image = UIImage(named: \"photo.png\")\n\t\tself.view.addSubview(view!)\n\t}\n\n}\n\nextension ViewController: MDCSwipeToChooseDelegate {\n\n\t// This is called when a user didn't fully swipe left or right.\n\tfunc viewDidCancelSwipe(_ view: UIView) -\u003e Void{\n\t\tprint(\"Couldn't decide, huh?\")\n\t}\n\n\t// Sent before a choice is made. Cancel the choice by returning `false`. Otherwise return `true`.\n\tfunc view(_ view: UIView, shouldBeChosenWith: MDCSwipeDirection) -\u003e Bool {\n\t\tif shouldBeChosenWith == .left {\n\t\t\treturn true\n\t\t} else {\n\t\t\t// Snap the view back and cancel the choice.\n\t\t\tUIView.animate(withDuration: 0.16, animations: { () -\u003e Void in\n\t\t\t\tview.transform = CGAffineTransform.identity\n\t\t\t\tview.center = view.superview!.center\n\t\t\t})\n\t\t\treturn false\n\t\t}\n\t}\n\n\t// This is called when a user swipes the view fully left or right.\n\tfunc view(_ view: UIView, wasChosenWith: MDCSwipeDirection) -\u003e Void {\n\t\tif wasChosenWith == .left {\n\t\t\tprint(\"Photo deleted!\")\n\t\t} else {\n\t\t\tprint(\"Photo saved!\")\n\t\t}\n\t}\n}\n```\nIf you're using CocoaPods 0.36+ (perhaps because you want to include pods that contain Swift code) and you've included the use_frameworks! directive in your Podfile, then you've converted all your pods (including MDCSwipeToChoose) into frameworks. Therefore, you'll need to include the line\n\n```swift\nimport MDCSwipeToChoose\n```\n...in your Swift files (even if you're using a bridging header).\n\n## More Generic Swiping\n\nYou don't have to use a subclass of `MDCChooseView`. You can use the `mdc_swipeToChooseSetup:` method on any `UIView` to enable swipe-to-choose.\n\nIn the following example, we adjust the opacity of a `UIWebView` when it's panned left and right.\n\n```objc\n#import \u003cMDCSwipeToChoose/MDCSwipeToChoose.h\u003e\n\n// ... in a view controller\n\n- (void)viewDidLoad {\n    [super viewDidLoad];\n\n    MDCSwipeOptions *options = [MDCSwipeOptions new];\n    options.delegate = self;\n    options.onPan = ^(MDCPanState *state){\n        switch (state.direction) {\n            case MDCSwipeDirectionLeft:\n                self.webView.alpha = 0.5f - state.thresholdRatio;\n                break;\n            case MDCSwipeDirectionRight:\n                self.webView.alpha = 0.5f + state.thresholdRatio;\n                break;\n            case MDCSwipeDirectionNone:\n                self.webView.alpha = 0.5f;\n                break;\n        }\n    };\n    [self.webView mdc_swipeToChooseSetup:options];\n}\n```\n##Swiping in Swift\n\n\nThe following is an example of how you can use `MDCSwipeToChooseView` to display a photo in swift. The user can choose to delete it by swiping left, or save it by swiping right.\n\nFirst you must create a BridgingHeader.h file\n```objc\n#ifndef ProjectName_BridgingHeader_h\n#define ProjectName_BridgingHeader_h\n\n\n#import \u003cUIKit/UIKit.h\u003e\n#import \u003cMDCSwipeToChoose/MDCSwipeToChoose.h\u003e\n\n#endif\n```\nYou must then add the bridging header file to the project by navigating to Build Settings then searching for 'Bridging Header'. Double click the field and type: ProjectName/BridgingHeader.h as the value\n\n```swift\n// Creating and Customizing a MDCSwipeToChooseView\n\noverride func viewDidLoad(){\n    super.viewDidLoad()\n\n    // You can customize MDCSwipeToChooseView using MDCSwipeToChooseViewOptions.\n    let options:MDCSwipeToChooseViewOptions = MDCSwipeToChooseViewOptions()\n    options.delegate = self\n    options.likedText = \"Keep\"\n    options.likedColor = UIColor.blue\n    options.nopeText = \"Delete\"\n    options.nopeColor = UIColor.red\n    options.onPan = { state -\u003e Void in\n    if (state?.thresholdRatio == 1.0 \u0026\u0026 state?.direction == .left) {\n        print(\"Let go now to delete the photo!\")\n    }\n}\n\nlet view:MDCSwipeToChooseView = MDCSwipeToChooseView(frame:self.view.bounds, options:options)\n    view.imageView.image = UIImage(named:\"photo\")\n    self.view.addSubview(view)\n}\n\n// MDCSwipeToChooseDelegate Callbacks\n\n// This is called when a user didn't fully swipe left or right.\nfunc viewDidCancelSwipe(_ view: UIView) -\u003e Void {\n    print(\"Couldn't decide, huh?\")\n}\n\n// Sent before a choice is made. Cancel the choice by returning `false`. Otherwise return `true`.\nfunc view(_ view:UIView, shouldBeChosenWith: MDCSwipeDirection) -\u003e Bool {\n    if (shouldBeChosenWith == .left) {\n        return true\n    } else {\n        // Snap the view back and cancel the choice.\n        UIView.animate(withDuration: 0.16, animations: { () -\u003e Void in\n            view.transform = CGAffineTransform.identity\n            view.center = self.view.center\n        })\n    return false\n    }\n}\n\n// This is called when a user swipes the view fully left or right.\nfunc view(_ view: UIView, wasChosenWith: MDCSwipeDirection) -\u003e Void{\n    if (wasChosenWith == .left) {\n        print(\"Photo deleted!\")\n    } else {\n        print(\"Photo saved!\")\n    }\n}\n\n```\n\n### Swiping programmatically\nAs of version 0.2.0, you may also swipe a view programmatically:\n\n#### Objective-C\n```objc\nself.swipeToChooseView(mdc_swipe:MDCSwipeDirection.Left)\n[self.swipeToChooseView mdc_swipe:MDCSwipeDirectionLeft];\n```\n\n#### Swift\n```swift\nself.swipeToChooseView.mdc_swipe(.left)\n```\n\n### Disable swiping gesture\nYou may also disable the swiping gesture and only allowed to swipe programmatically\n\n#### Objective-C\n```objc\nMDCSwipeToChooseViewOptions *options = [MDCSwipeToChooseViewOptions new];\noptions.swipeEnabled = NO;\n```\n\n#### Swift\n```swift\nlet options = MDCSwipeToChooseViewOptions()\noptions.swipeEnabled = false\n```\n\n\n## License\n\nAll the source code is distributed under the [MIT license](http://www.opensource.org/licenses/mit-license.php). See the LICENSE file for details. The license does not apply to the images used in the sample apps.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmodocache%2FMDCSwipeToChoose","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmodocache%2FMDCSwipeToChoose","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmodocache%2FMDCSwipeToChoose/lists"}