MGSwipeTableCell
MGSwipeTableCell 是一个易于使用的 UITableViewCell 子类,它允许显示具有各种转换效果的可滑动按钮。
此库与所有生成 UITableViewCell 的不同方式兼容:系统预定义样式、程序生成的单元格、从 xib 加载的单元格以及故事板中的原型单元格。如果您需要,可以使用自动布局。
适用于 iOS >= 5.0。在 iPhone 和 iPad 上的所有 iOS 版本上进行了测试:iOS 7、iOS 8、iOS 9、iOS 10、iOS 11、iOS 12、iOS 13、iOS 14。
转换演示
边界转换
剪切转换
3D过渡
静态过渡
拖动过渡
API参考
查看MGSwipeTableCell.h
头文件,以获取该类功能的全面概述。
查看MailAppDemo
项目,这是一个模仿苹果邮箱应用程序的全项目(使用Objective-C编写)
查看MailAppDemoSwift
项目,这是一个模仿苹果邮箱应用程序的全项目(使用Swift编写)
查看SpotifyDemo
项目,这是一个模仿Spotify应用程序滑动样式的全项目
查看MGSwipeDemo
项目,这是一个可以在真实设备/模拟器上测试各种过渡效果的完整项目。
设置您的项目
您可以使用CocoaPods将MGSwipeTableCell集成到您的项目中
pod 'MGSwipeTableCell'
您可以使用Carthage将MGSwipeTableCell集成到您的项目中。只需将此依赖关系添加到您的Cartfile中
github "MortimerGoro/MGSwipeTableCell"
您可以使用Swift Package Manager将MGSwipeTableCell集成到您的项目中
.package(url: "https://github.com/MortimerGoro/MGSwipeTableCell.git", from: "1.6.0")
用法
基本
在项目中集成 MGSwipeTableCell 非常简单。基本上,你只需要从 UITableViewCell 继承 MGSwipeTableCell,或者直接使用 iOS 预定义的单元格样式实例化 MGSwipeTableCell。你可以像以前一样布局单元格内容,MGSwipeTableCell 不会强制你更改布局。
以下是一个使用 iOS 预定义样式的 MGSwipeTableCell 的示例。你可以为 cell.leftButtons 和/或 cell.rightButtons 属性设置按钮数组。MGSwipeButton 是一个便利类,你不必使用它。你可以使用自己的 UIButtons 或 UIViews。你可以通过 leftSwipeSettings 和/或 rightSwipeSettings 属性配置过渡(和滑动阈值)
Objective-C
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * reuseIdentifier = @"programmaticCell";
MGSwipeTableCell * cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (!cell) {
cell = [[MGSwipeTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
}
cell.textLabel.text = @"Title";
cell.detailTextLabel.text = @"Detail text";
cell.delegate = self; //optional
//configure left buttons
cell.leftButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[UIImage imageNamed:@"check.png"] backgroundColor:[UIColor greenColor]],
[MGSwipeButton buttonWithTitle:@"" icon:[UIImage imageNamed:@"fav.png"] backgroundColor:[UIColor blueColor]]];
cell.leftSwipeSettings.transition = MGSwipeTransition3D;
//configure right buttons
cell.rightButtons = @[[MGSwipeButton buttonWithTitle:@"Delete" backgroundColor:[UIColor redColor]],
[MGSwipeButton buttonWithTitle:@"More" backgroundColor:[UIColor lightGrayColor]]];
cell.rightSwipeSettings.transition = MGSwipeTransition3D;
return cell;
}
Swift
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let reuseIdentifier = "programmaticCell"
var cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! MGSwipeTableCell
cell.textLabel!.text = "Title"
cell.detailTextLabel!.text = "Detail text"
cell.delegate = self //optional
//configure left buttons
cell.leftButtons = [MGSwipeButton(title: "", icon: UIImage(named:"check.png"), backgroundColor: .green),
MGSwipeButton(title: "", icon: UIImage(named:"fav.png"), backgroundColor: .blue)]
cell.leftSwipeSettings.transition = .rotate3D
//configure right buttons
cell.rightButtons = [MGSwipeButton(title: "Delete", backgroundColor: .red),
MGSwipeButton(title: "More",backgroundColor: .lightGray)]
cell.rightSwipeSettings.transition = .rotate3D
return cell
}
为了监听按钮点击事件,你可以实现可选的 MGSwipeTableCellDelegate,或者如果你太懒不想这么做,MGSwipeButton 类自带便利的回调块;)
Objective-c
[MGSwipeButton buttonWithTitle:@"More" backgroundColor:[UIColor lightGrayColor] callback:^BOOL(MGSwipeTableCell *sender) {
NSLog(@"Convenience callback for swipe buttons!");
}]
Swift
MGSwipeButton(title: "Delete", backgroundColor: .red) {
(sender: MGSwipeTableCell!) -> Bool in
print("Convenience callback for swipe buttons!")
return true
}
委托
MGSwipeTableCellDelegate是一个可选委托,用于配置滑动按钮或接收触发的动作或其他事件。在创建单元格时,可以在行内配置按钮,而不是使用此委托,但使用委托可以提高内存使用效率,因为按钮只在需要时创建。
@protocol MGSwipeTableCellDelegate <NSObject>
@optional
/**
* Delegate method to enable/disable swipe gestures
* @return YES if swipe is allowed
**/
-(BOOL) swipeTableCell:(MGSwipeTableCell*) cell canSwipe:(MGSwipeDirection) direction;
/**
* Delegate method invoked when the current swipe state changes
@param state the current Swipe State
@param gestureIsActive YES if the user swipe gesture is active. No if the uses has already ended the gesture
**/
-(void) swipeTableCell:(MGSwipeTableCell*) cell didChangeSwipeState:(MGSwipeState) state gestureIsActive:(BOOL) gestureIsActive;
/**
* Called when the user clicks a swipe button or when a expandable button is automatically triggered
* @return YES to autohide the current swipe buttons
**/
-(BOOL) swipeTableCell:(MGSwipeTableCell*) cell tappedButtonAtIndex:(NSInteger) index direction:(MGSwipeDirection)direction fromExpansion:(BOOL) fromExpansion;
/**
* Delegate method to setup the swipe buttons and swipe/expansion settings
* Buttons can be any kind of UIView but it's recommended to use the convenience MGSwipeButton class
* Setting up buttons with this delegate instead of using cell properties improves memory usage because buttons are only created in demand
* @param swipeTableCell the UITableViewCell to configure. You can get the indexPath using [tableView indexPathForCell:cell]
* @param direction The swipe direction (left to right or right to left)
* @param swipeSettings instance to configure the swipe transition and setting (optional)
* @param expansionSettings instance to configure button expansions (optional)
* @return Buttons array
**/
-(NSArray*) swipeTableCell:(MGSwipeTableCell*) cell swipeButtonsForDirection:(MGSwipeDirection)direction
swipeSettings:(MGSwipeSettings*) swipeSettings expansionSettings:(MGSwipeExpansionSettings*) expansionSettings;
@end
可展开按钮
默认情况下,按钮不可展开。您可以使用cell.leftExpansion和cell.rightExpansion属性设置可展开按钮
当用户结束滑动手势并且展开状态激活时(可通过阈值值进行配置),自动触发可展开按钮的事件。触发的可展开按钮可以弹回初始位置或填充整个UITableViewCell,您可以使用fillOnTrigger属性选择所需的动画。
@interface MGSwipeExpansionSettings: NSObject
/** index of the expandable button (in the left or right buttons arrays) */
@property (nonatomic, assign) NSInteger buttonIndex;
/** if true the button fills the cell on trigger, else it bounces back to its initial position */
@property (nonatomic, assign) BOOL fillOnTrigger;
/** Size proportional threshold to trigger the expansion button. Default value 1.5 */
@property (nonatomic, assign) CGFloat threshold;
@end
圆角和滑动按钮
MGSwipeTableCell支持圆角。示例
cell.layer.cornerRadius = 50
cell.backgroundColor = UIColor.gray
cell.clipsToBounds = true
cell.swipeBackgroundColor = UIColor.gray
许可协议
MIT许可协议 (MIT)
版权所有 (c) 2014 Imanol Fernandez @MortimerGoro
特此授予任何获得本软件及其相关文档副本(“软件”)的个人无限制地使用该软件的自由,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许将软件提供给获得软件的个人,以供其使用,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何形式的明示或暗示保证,包括但不限于适销性、特定用途适用性和非侵犯性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论基于合同、侵权或其他原因,是否因软件的产生、来源或在软件的使用或其他交易中产生。