这个库基于 MDCSwipeToChoose 开发,正如名称所示,它可以反转。
使用该库时,只需在 Podfile 中添加 "pod 'LFReversibleTinderView'",然后执行 "pod install" 并导入 "LFReversibleTinderView.h",即可开始使用。
在 LikedOrNope 示例中,ChoosePersonView 是 LFReversibleTinderSubview 的子视图。您首先需要设置您的子视图。
// set up views
for (int i = 0; i < 4; i++)
{
ChoosePersonView* view = [[ChoosePersonView alloc]
initWithFrame:CGRectMake(0, 0, 280, 360) person:self.people[i] options:nil];
[views addObject:view];
}
然后设置 Tinder 样式的视图
// set up swipe view
LFReversibleTinderView* view_swipe = [[LFReversibleTinderView alloc] initWithFrame:CGRectMake(0, 60, 320, 320)];
view_swipe.swipe_views = views;
并将其作为子视图添加
[self.view addSubview:view_swipe];
完成。请注意,下面的 MDCSwipeToChoose 部分可能与该库不兼容,如果您不希望使用反转功能,请获取原始版本。- https://github.com/superarts/LFramework
功能就在那里,但这个库仍在调整中,所以请在自己的风险下使用它。如果您也需要一些方便的助手,请查看我们的 LFramework! - https://github.com/superarts/LFramework
像 Tinder.app 一样滑动以“喜欢”或“不喜欢”任何视图。在几分钟内,而非几小时内构建闪存卡应用程序、图片查看器等!
UIView+MDCSwipeToChoose
向任何 UIView
添加滑动手势和回调。MDCSwipeToChooseView
可以用几行代码就创建一个几乎与 Tinder.app 一致的用户界面。您可以在此处查看一些关于此库设计决策的幻灯片 在这里。
查看 示例应用程序,了解如何使用 MDCSwipeToChooseView
构建上图中的 UI。
注意:在构建示例应用程序之前,您必须在
Examples/LikedOrNope
目录中运行pod install
。
每个公开的类在其头文件中都有文档。
以下是一个使用 MDCSwipeToChooseView
显示图片的示例。用户可以通过向左滑动选择删除它,或者通过向右滑动保存它。
#import <MDCSwipeToChoose/MDCSwipeToChoose.h>
// ... in a view controller
#pragma mark - Creating and Customizing a MDCSwipeToChooseView
- (void)viewDidLoad {
[super viewDidLoad];
// You can customize MDCSwipeToChooseView using MDCSwipeToChooseViewOptions.
MDCSwipeToChooseViewOptions *options = [MDCSwipeToChooseViewOptions new];
options.delegate = self;
options.likedText = @"Keep";
options.likedColor = [UIColor blueColor];
options.nopeText = @"Delete";
options.onPan = ^(MDCPanState *state){
if (state.thresholdRatio == 1.f && state.direction == MDCSwipeDirectionLeft) {
NSLog(@"Let go now to delete the photo!");
}
};
MDCSwipeToChooseView *view = [[MDCSwipeToChooseView alloc] initWithFrame:self.view.bounds
options:options];
view.imageView.image = [UIImage imageNamed:@"photo"];
[self.view addSubview:view];
}
#pragma mark - MDCSwipeToChooseDelegate Callbacks
// This is called when a user didn't fully swipe left or right.
- (void)viewDidCancelSwipe:(UIView *)view {
NSLog(@"Couldn't decide, huh?");
}
// This is called then a user swipes the view fully left or right.
- (void)view:(UIView *)view wasChosenWithDirection:(MDCSwipeDirection)direction {
if (direction == MDCSwipeDirectionLeft) {
NSLog(@"Photo deleted!");
} else {
NSLog(@"Photo saved!");
}
}
从版本 0.2.0 开始,您也可以编程方式滑动视图
[self.swipeToChooseView mdc_swipe:MDCSwipeDirectionLeft];
您不必使用 MDCChooseView
的子类。您可以通过任何 UIView
的 mdc_swipeToChooseSetup:
方法启用滑动到选择。
在以下示例中,我们调整当向左或向右拖拽时 UIWebView
的不透明度。
#import <MDCSwipeToChoose/MDCSwipeToChoose.h>
// ... in a view controller
- (void)viewDidLoad {
[super viewDidLoad];
MDCSwipeOptions *options = [MDCSwipeOptions new];
options.delegate = self;
options.onPan = ^(MDCPanState *state){
switch (state.direction) {
case MDCSwipeDirectionLeft:
self.webView.alpha = 0.5f - state.thresholdRatio;
break;
case MDCSwipeDirectionRight:
self.webView.alpha = 0.5f + state.thresholdRatio;
break;
case MDCSwipeDirectionNone:
self.webView.alpha = 0.5f;
break;
}
};
[self.webView mdc_swipeToChooseSetup:options];
}
所有源代码均在MIT许可协议下分发。请参阅LICENSE文件获取详细信息。该许可协议不适用于示例应用中使用的图像。