add Pad-bak

This commit is contained in:
life
2019-03-26 16:30:36 +08:00
parent 1fa04c6a48
commit 2360d795a8
517 changed files with 43568 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
Copyright (c) 2013 Christopher Wendel
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,249 @@
SWTableViewCell
===============
<p align="center"><img src="http://i.imgur.com/njKCjK8.gif"/></p>
An easy-to-use UITableViewCell subclass that implements a swipeable content view which exposes utility buttons (similar to iOS 7 Mail Application)
##Usage
In your Podfile:
<pre>pod 'SWTableViewCell', '~> 0.3.7'</pre>
Or just clone this repo and manually add source to project
##Functionality
###Right Utility Buttons
Utility buttons that become visible on the right side of the Table View Cell when the user swipes left. This behavior is similar to that seen in the iOS apps Mail and Reminders.
<p align="center"><img src="http://i.imgur.com/gDZFRpr.gif"/></p>
###Left Utility Buttons
Utility buttons that become visible on the left side of the Table View Cell when the user swipes right.
<p align="center"><img src="http://i.imgur.com/qt6aISz.gif"/></p>
###Features
* Dynamic utility button scalling. As you add more buttons to a cell, the other buttons on that side get smaller to make room
* Smart selection: The cell will pick up touch events and either scroll the cell back to center or fire the delegate method `- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath`
<p align="center"><img src="http://i.imgur.com/TYGx9h8.gif"/></p>
So the cell will not be considered selected when the user touches the cell while utility buttons are visible, instead the cell will slide back into place (same as iOS 7 Mail App functionality)
* Create utilty buttons with either a title or an icon along with a RGB color
* Tested on iOS 6.1 and above, including iOS 7
##Usage
###Standard Table View Cells
In your `tableView:cellForRowAtIndexPath:` method you set up the SWTableView cell and add an arbitrary amount of utility buttons to it using the included `NSMutableArray+SWUtilityButtons` category.
```objc
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.leftUtilityButtons = [self leftButtons];
cell.rightUtilityButtons = [self rightButtons];
cell.delegate = self;
}
NSDate *dateObject = _testArray[indexPath.row];
cell.textLabel.text = [dateObject description];
cell.detailTextLabel.text = @"Some detail text";
return cell;
}
- (NSArray *)rightButtons
{
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
[rightUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0]
title:@"More"];
[rightUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]
title:@"Delete"];
return rightUtilityButtons;
}
- (NSArray *)leftButtons
{
NSMutableArray *leftUtilityButtons = [NSMutableArray new];
[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:0.07 green:0.75f blue:0.16f alpha:1.0]
icon:[UIImage imageNamed:@"check.png"]];
[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:1.0f blue:0.35f alpha:1.0]
icon:[UIImage imageNamed:@"clock.png"]];
[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:0.231f blue:0.188f alpha:1.0]
icon:[UIImage imageNamed:@"cross.png"]];
[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:0.55f green:0.27f blue:0.07f alpha:1.0]
icon:[UIImage imageNamed:@"list.png"]];
return leftUtilityButtons;
}
```
###Custom Table View Cells
Thanks to [Matt Bowman](https://github.com/MattCBowman) you can now create custom table view cells using Interface Builder that have the capabilities of an SWTableViewCell
The first step is to design your cell either in a standalone nib or inside of a
table view using prototype cells. Make sure to set the custom class on the
cell in interface builder to the subclass you made for it:
<p align="center"><img src="http://i.imgur.com/mfrT1Ex.png"/></p>
Then set the cell reuse identifier:
<p align="center"><img src="http://i.imgur.com/dlmArZ1.png"/></p>
When writing your custom table view cell's code, make sure your cell is a
subclass of SWTableViewCell:
```objc
#import <SWTableViewCell.h>
@interface MyCustomTableViewCell : SWTableViewCell
@property (weak, nonatomic) UILabel *customLabel;
@property (weak, nonatomic) UIImageView *customImageView;
@end
```
If you are using a separate nib and not a prototype cell, you'll need to be sure to register the nib in your table view:
```objc
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"MyCustomTableViewCellNibFileName" bundle:nil] forCellReuseIdentifier:@"MyCustomCell"];
}
```
Then, in the `tableView:cellForRowAtIndexPath:` method of your `UITableViewDelegate` (usually your view controller), initialize your custom cell:
```objc
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *cellIdentifier = @"MyCustomCell";
MyCustomTableViewCell *cell = (MyCustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier
forIndexPath:indexPath];
cell.leftUtilityButtons = [self leftButtons];
cell.rightUtilityButtons = [self rightButtons];
cell.delegate = self;
cell.customLabel.text = @"Some Text";
cell.customImageView.image = [UIImage imageNamed:@"MyAwesomeTableCellImage"];
[cell setCellHeight:cell.frame.size.height];
return cell;
}
```
###Delegate
The delegate `SWTableViewCellDelegate` is used by the developer to find out which button was pressed. There are five methods:
```objc
// click event on left utility button
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index;
// click event on right utility button
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index;
// utility button open/close event
- (void)swipeableTableViewCell:(SWTableViewCell *)cell scrollingToState:(SWCellState)state;
// prevent multiple cells from showing utilty buttons simultaneously
- (BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell;
// prevent cell(s) from displaying left/right utility buttons
- (BOOL)swipeableTableViewCell:(SWTableViewCell *)cell canSwipeToState:(SWCellState)state;
```
The index signifies which utility button the user pressed, for each side the button indices are ordered from right to left 0...n
####Example
```objc
#pragma mark - SWTableViewDelegate
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index {
switch (index) {
case 0:
NSLog(@"check button was pressed");
break;
case 1:
NSLog(@"clock button was pressed");
break;
case 2:
NSLog(@"cross button was pressed");
break;
case 3:
NSLog(@"list button was pressed");
default:
break;
}
}
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index {
switch (index) {
case 0:
NSLog(@"More button was pressed");
break;
case 1:
{
// Delete button was pressed
NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
[_testArray removeObjectAtIndex:cellIndexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[cellIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
default:
break;
}
}
```
(This is all code from the included example project)
###Gotchas
#### Seperator Insets
* If you have left utility button on iOS 7, I recommend changing your Table View's seperatorInset so the seperator stretches the length of the screen
<pre> tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0); </pre>
##Contributing
Use [Github issues](https://github.com/cewendel/SWTableViewCell/issues) to track bugs and feature requests.
##Contact
Chris Wendel
- http://twitter.com/CEWendel
## Licence
MIT

View File

@@ -0,0 +1,25 @@
//
// NSMutableArray+SWUtilityButtons.h
// SWTableViewCell
//
// Created by Matt Bowman on 11/27/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSMutableArray (SWUtilityButtons)
- (void)sw_addUtilityButtonWithColor:(UIColor *)color title:(NSString *)title;
- (void)sw_addUtilityButtonWithColor:(UIColor *)color attributedTitle:(NSAttributedString *)title;
- (void)sw_addUtilityButtonWithColor:(UIColor *)color icon:(UIImage *)icon;
- (void)sw_addUtilityButtonWithColor:(UIColor *)color normalIcon:(UIImage *)normalIcon selectedIcon:(UIImage *)selectedIcon;
@end
@interface NSArray (SWUtilityButtons)
- (BOOL)sw_isEqualToButtons:(NSArray *)buttons;
@end

View File

@@ -0,0 +1,92 @@
//
// NSMutableArray+SWUtilityButtons.m
// SWTableViewCell
//
// Created by Matt Bowman on 11/27/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import "NSMutableArray+SWUtilityButtons.h"
@implementation NSMutableArray (SWUtilityButtons)
- (void)sw_addUtilityButtonWithColor:(UIColor *)color title:(NSString *)title
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = color;
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button.titleLabel setAdjustsFontSizeToFitWidth:YES];
[self addObject:button];
}
- (void)sw_addUtilityButtonWithColor:(UIColor *)color attributedTitle:(NSAttributedString *)title
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = color;
[button setAttributedTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self addObject:button];
}
- (void)sw_addUtilityButtonWithColor:(UIColor *)color icon:(UIImage *)icon
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = color;
[button setImage:icon forState:UIControlStateNormal];
[self addObject:button];
}
- (void)sw_addUtilityButtonWithColor:(UIColor *)color normalIcon:(UIImage *)normalIcon selectedIcon:(UIImage *)selectedIcon {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = color;
[button setImage:normalIcon forState:UIControlStateNormal];
[button setImage:selectedIcon forState:UIControlStateHighlighted];
[button setImage:selectedIcon forState:UIControlStateSelected];
[self addObject:button];
}
@end
@implementation NSArray (SWUtilityButtons)
- (BOOL)sw_isEqualToButtons:(NSArray *)buttons
{
buttons = [buttons copy];
if (!buttons || self.count != buttons.count) return NO;
for (NSUInteger idx = 0; idx < self.count; idx++) {
id buttonA = self[idx];
id buttonB = buttons[idx];
if (![buttonA isKindOfClass:[UIButton class]] || ![buttonB isKindOfClass:[UIButton class]]) return NO;
if (![[self class] sw_button:buttonA isEqualToButton:buttonB]) return NO;
}
return YES;
}
+ (BOOL)sw_button:(UIButton *)buttonA isEqualToButton:(UIButton *)buttonB
{
if (!buttonA || !buttonB) return NO;
UIColor *backgroundColorA = buttonA.backgroundColor;
UIColor *backgroundColorB = buttonB.backgroundColor;
BOOL haveEqualBackgroundColors = (!backgroundColorA && !backgroundColorB) || [backgroundColorA isEqual:backgroundColorB];
NSString *titleA = [buttonA titleForState:UIControlStateNormal];
NSString *titleB = [buttonB titleForState:UIControlStateNormal];
BOOL haveEqualTitles = (!titleA && !titleB) || [titleA isEqualToString:titleB];
UIImage *normalIconA = [buttonA imageForState:UIControlStateNormal];
UIImage *normalIconB = [buttonB imageForState:UIControlStateNormal];
BOOL haveEqualNormalIcons = (!normalIconA && !normalIconB) || [normalIconA isEqual:normalIconB];
UIImage *selectedIconA = [buttonA imageForState:UIControlStateSelected];
UIImage *selectedIconB = [buttonB imageForState:UIControlStateSelected];
BOOL haveEqualSelectedIcons = (!selectedIconA && !selectedIconB) || [selectedIconA isEqual:selectedIconB];
return haveEqualBackgroundColors && haveEqualTitles && haveEqualNormalIcons && haveEqualSelectedIcons;
}
@end

View File

@@ -0,0 +1,13 @@
//
// SWCellScrollView.h
// SWTableViewCell
//
// Created by Matt Bowman on 11/27/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SWCellScrollView : UIScrollView <UIGestureRecognizerDelegate>
@end

View File

@@ -0,0 +1,42 @@
//
// SWCellScrollView.m
// SWTableViewCell
//
// Created by Matt Bowman on 11/27/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import "SWCellScrollView.h"
@implementation SWCellScrollView
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer == self.panGestureRecognizer) {
CGPoint translation = [(UIPanGestureRecognizer*)gestureRecognizer translationInView:gestureRecognizer.view];
return fabs(translation.y) <= fabs(translation.x);
} else {
return YES;
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
// Find out if the user is actively scrolling the tableView of which this is a member.
// If they are, return NO, and don't let the gesture recognizers work simultaneously.
//
// This works very well in maintaining user expectations while still allowing for the user to
// scroll the cell sideways when that is their true intent.
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
// Find the current scrolling velocity in that view, in the Y direction.
CGFloat yVelocity = [(UIPanGestureRecognizer*)gestureRecognizer velocityInView:gestureRecognizer.view].y;
// Return YES iff the user is not actively scrolling up.
return fabs(yVelocity) <= 0.25;
}
return YES;
}
@end

View File

@@ -0,0 +1,15 @@
//
// SWLongPressGestureRecognizer.h
// SWTableViewCell
//
// Created by Matt Bowman on 11/27/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
@interface SWLongPressGestureRecognizer : UILongPressGestureRecognizer
@end

View File

@@ -0,0 +1,33 @@
//
// SWLongPressGestureRecognizer.m
// SWTableViewCell
//
// Created by Matt Bowman on 11/27/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import "SWLongPressGestureRecognizer.h"
@implementation SWLongPressGestureRecognizer
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
self.state = UIGestureRecognizerStateFailed;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
self.state = UIGestureRecognizerStateFailed;
}
@end

View File

@@ -0,0 +1,52 @@
//
// SWTableViewCell.h
// SWTableViewCell
//
// Created by Chris Wendel on 9/10/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
#import "SWCellScrollView.h"
#import "SWLongPressGestureRecognizer.h"
#import "SWUtilityButtonTapGestureRecognizer.h"
#import "NSMutableArray+SWUtilityButtons.h"
@class SWTableViewCell;
typedef NS_ENUM(NSInteger, SWCellState)
{
kCellStateCenter,
kCellStateLeft,
kCellStateRight,
};
@protocol SWTableViewCellDelegate <NSObject>
@optional
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index;
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index;
- (void)swipeableTableViewCell:(SWTableViewCell *)cell scrollingToState:(SWCellState)state;
- (BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell;
- (BOOL)swipeableTableViewCell:(SWTableViewCell *)cell canSwipeToState:(SWCellState)state;
- (void)swipeableTableViewCellDidEndScrolling:(SWTableViewCell *)cell;
@end
@interface SWTableViewCell : UITableViewCell
@property (nonatomic, copy) NSArray *leftUtilityButtons;
@property (nonatomic, copy) NSArray *rightUtilityButtons;
@property (nonatomic, weak) id <SWTableViewCellDelegate> delegate;
- (void)setRightUtilityButtons:(NSArray *)rightUtilityButtons WithButtonWidth:(CGFloat) width;
- (void)setLeftUtilityButtons:(NSArray *)leftUtilityButtons WithButtonWidth:(CGFloat) width;
- (void)hideUtilityButtonsAnimated:(BOOL)animated;
- (void)showLeftUtilityButtonsAnimated:(BOOL)animated;
- (void)showRightUtilityButtonsAnimated:(BOOL)animated;
- (BOOL)isUtilityButtonsHidden;
@end

View File

@@ -0,0 +1,800 @@
//
// SWTableViewCell.m
// SWTableViewCell
//
// Created by Chris Wendel on 9/10/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import "SWTableViewCell.h"
#import "SWUtilityButtonView.h"
static NSString * const kTableViewCellContentView = @"UITableViewCellContentView";
#define kSectionIndexWidth 15
#define kAccessoryTrailingSpace 15
#define kLongPressMinimumDuration 0.16f
@interface SWTableViewCell () <UIScrollViewDelegate, UIGestureRecognizerDelegate>
@property (nonatomic, weak) UITableView *containingTableView;
@property (nonatomic, strong) UIPanGestureRecognizer *tableViewPanGestureRecognizer;
@property (nonatomic, assign) SWCellState cellState; // The state of the cell within the scroll view, can be left, right or middle
@property (nonatomic, assign) CGFloat additionalRightPadding;
@property (nonatomic, strong) UIScrollView *cellScrollView;
@property (nonatomic, strong) SWUtilityButtonView *leftUtilityButtonsView, *rightUtilityButtonsView;
@property (nonatomic, strong) UIView *leftUtilityClipView, *rightUtilityClipView;
@property (nonatomic, strong) NSLayoutConstraint *leftUtilityClipConstraint, *rightUtilityClipConstraint;
@property (nonatomic, strong) UILongPressGestureRecognizer *longPressGestureRecognizer;
@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer;
- (CGFloat)leftUtilityButtonsWidth;
- (CGFloat)rightUtilityButtonsWidth;
- (CGFloat)utilityButtonsPadding;
- (CGPoint)contentOffsetForCellState:(SWCellState)state;
- (void)updateCellState;
- (BOOL)shouldHighlight;
@end
@implementation SWTableViewCell {
UIView *_contentCellView;
BOOL layoutUpdating;
}
#pragma mark Initializers
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self initializer];
}
return self;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
[self initializer];
}
return self;
}
- (void)initializer
{
layoutUpdating = NO;
// Set up scroll view that will host our cell content
self.cellScrollView = [[SWCellScrollView alloc] init];
self.cellScrollView.translatesAutoresizingMaskIntoConstraints = NO;
self.cellScrollView.delegate = self;
self.cellScrollView.showsHorizontalScrollIndicator = NO;
self.cellScrollView.scrollsToTop = NO;
self.cellScrollView.scrollEnabled = YES;
_contentCellView = [[UIView alloc] init];
[self.cellScrollView addSubview:_contentCellView];
// Add the cell scroll view to the cell
UIView *contentViewParent = self;
UIView *clipViewParent = self.cellScrollView;
if (![NSStringFromClass([[self.subviews objectAtIndex:0] class]) isEqualToString:kTableViewCellContentView])
{
// iOS 7
contentViewParent = [self.subviews objectAtIndex:0];
clipViewParent = self;
}
NSArray *cellSubviews = [contentViewParent subviews];
[self insertSubview:self.cellScrollView atIndex:0];
for (UIView *subview in cellSubviews)
{
[_contentCellView addSubview:subview];
}
// Set scroll view to perpetually have same frame as self. Specifying relative to superview doesn't work, since the latter UITableViewCellScrollView has different behaviour.
[self addConstraints:@[
[NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:self.cellScrollView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0],
]];
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTapped:)];
self.tapGestureRecognizer.cancelsTouchesInView = NO;
self.tapGestureRecognizer.delegate = self;
[self.cellScrollView addGestureRecognizer:self.tapGestureRecognizer];
self.longPressGestureRecognizer = [[SWLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewPressed:)];
self.longPressGestureRecognizer.cancelsTouchesInView = NO;
self.longPressGestureRecognizer.minimumPressDuration = kLongPressMinimumDuration;
self.longPressGestureRecognizer.delegate = self;
[self.cellScrollView addGestureRecognizer:self.longPressGestureRecognizer];
// Create the left and right utility button views, as well as vanilla UIViews in which to embed them. We can manipulate the latter in order to effect clipping according to scroll position.
// Such an approach is necessary in order for the utility views to sit on top to get taps, as well as allow the backgroundColor (and private UITableViewCellBackgroundView) to work properly.
self.leftUtilityClipView = [[UIView alloc] init];
self.leftUtilityClipConstraint = [NSLayoutConstraint constraintWithItem:self.leftUtilityClipView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0];
self.leftUtilityButtonsView = [[SWUtilityButtonView alloc] initWithUtilityButtons:nil
parentCell:self
utilityButtonSelector:@selector(leftUtilityButtonHandler:)];
self.rightUtilityClipView = [[UIView alloc] initWithFrame:self.bounds];
self.rightUtilityClipConstraint = [NSLayoutConstraint constraintWithItem:self.rightUtilityClipView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0];
self.rightUtilityButtonsView = [[SWUtilityButtonView alloc] initWithUtilityButtons:nil
parentCell:self
utilityButtonSelector:@selector(rightUtilityButtonHandler:)];
UIView *clipViews[] = { self.rightUtilityClipView, self.leftUtilityClipView };
NSLayoutConstraint *clipConstraints[] = { self.rightUtilityClipConstraint, self.leftUtilityClipConstraint };
UIView *buttonViews[] = { self.rightUtilityButtonsView, self.leftUtilityButtonsView };
NSLayoutAttribute alignmentAttributes[] = { NSLayoutAttributeRight, NSLayoutAttributeLeft };
for (NSUInteger i = 0; i < 2; ++i)
{
UIView *clipView = clipViews[i];
NSLayoutConstraint *clipConstraint = clipConstraints[i];
UIView *buttonView = buttonViews[i];
NSLayoutAttribute alignmentAttribute = alignmentAttributes[i];
clipConstraint.priority = UILayoutPriorityDefaultHigh;
clipView.translatesAutoresizingMaskIntoConstraints = NO;
clipView.clipsToBounds = YES;
[clipViewParent addSubview:clipView];
[self addConstraints:@[
// Pin the clipping view to the appropriate outer edges of the cell.
[NSLayoutConstraint constraintWithItem:clipView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:clipView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:clipView attribute:alignmentAttribute relatedBy:NSLayoutRelationEqual toItem:self attribute:alignmentAttribute multiplier:1.0 constant:0.0],
clipConstraint,
]];
[clipView addSubview:buttonView];
[self addConstraints:@[
// Pin the button view to the appropriate outer edges of its clipping view.
[NSLayoutConstraint constraintWithItem:buttonView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:clipView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:buttonView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:clipView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0],
[NSLayoutConstraint constraintWithItem:buttonView attribute:alignmentAttribute relatedBy:NSLayoutRelationEqual toItem:clipView attribute:alignmentAttribute multiplier:1.0 constant:0.0],
// Constrain the maximum button width so that at least a button's worth of contentView is left visible. (The button view will shrink accordingly.)
[NSLayoutConstraint constraintWithItem:buttonView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:-kUtilityButtonWidthDefault],
]];
}
}
static NSString * const kTableViewPanState = @"state";
- (void)removeOldTableViewPanObserver
{
[_tableViewPanGestureRecognizer removeObserver:self forKeyPath:kTableViewPanState];
}
- (void)dealloc
{
[self removeOldTableViewPanObserver];
}
- (void)setContainingTableView:(UITableView *)containingTableView
{
[self removeOldTableViewPanObserver];
_tableViewPanGestureRecognizer = containingTableView.panGestureRecognizer;
_containingTableView = containingTableView;
if (containingTableView)
{
// Check if the UITableView will display Indices on the right. If that's the case, add a padding
if ([_containingTableView.dataSource respondsToSelector:@selector(sectionIndexTitlesForTableView:)])
{
NSArray *indices = [_containingTableView.dataSource sectionIndexTitlesForTableView:_containingTableView];
self.additionalRightPadding = indices == nil ? 0 : kSectionIndexWidth;
}
_containingTableView.directionalLockEnabled = YES;
[self.tapGestureRecognizer requireGestureRecognizerToFail:_containingTableView.panGestureRecognizer];
[_tableViewPanGestureRecognizer addObserver:self forKeyPath:kTableViewPanState options:0 context:nil];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:kTableViewPanState] && object == _tableViewPanGestureRecognizer)
{
if(_tableViewPanGestureRecognizer.state == UIGestureRecognizerStateBegan)
{
CGPoint locationInTableView = [_tableViewPanGestureRecognizer locationInView:_containingTableView];
BOOL inCurrentCell = CGRectContainsPoint(self.frame, locationInTableView);
if(!inCurrentCell && _cellState != kCellStateCenter)
{
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:)])
{
if([self.delegate swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:self])
{
[self hideUtilityButtonsAnimated:YES];
}
}
}
}
}
}
- (void)setLeftUtilityButtons:(NSArray *)leftUtilityButtons
{
if (![_leftUtilityButtons sw_isEqualToButtons:leftUtilityButtons]) {
_leftUtilityButtons = leftUtilityButtons;
self.leftUtilityButtonsView.utilityButtons = leftUtilityButtons;
[self.leftUtilityButtonsView layoutIfNeeded];
[self layoutIfNeeded];
}
}
- (void)setLeftUtilityButtons:(NSArray *)leftUtilityButtons WithButtonWidth:(CGFloat) width
{
_leftUtilityButtons = leftUtilityButtons;
[self.leftUtilityButtonsView setUtilityButtons:leftUtilityButtons WithButtonWidth:width];
[self.leftUtilityButtonsView layoutIfNeeded];
[self layoutIfNeeded];
}
- (void)setRightUtilityButtons:(NSArray *)rightUtilityButtons
{
if (![_rightUtilityButtons sw_isEqualToButtons:rightUtilityButtons]) {
_rightUtilityButtons = rightUtilityButtons;
self.rightUtilityButtonsView.utilityButtons = rightUtilityButtons;
[self.rightUtilityButtonsView layoutIfNeeded];
[self layoutIfNeeded];
}
}
- (void)setRightUtilityButtons:(NSArray *)rightUtilityButtons WithButtonWidth:(CGFloat) width
{
_rightUtilityButtons = rightUtilityButtons;
[self.rightUtilityButtonsView setUtilityButtons:rightUtilityButtons WithButtonWidth:width];
[self.rightUtilityButtonsView layoutIfNeeded];
[self layoutIfNeeded];
}
#pragma mark - UITableViewCell overrides
- (void)didMoveToSuperview
{
self.containingTableView = nil;
UIView *view = self.superview;
do {
if ([view isKindOfClass:[UITableView class]])
{
self.containingTableView = (UITableView *)view;
break;
}
} while ((view = view.superview));
}
- (void)layoutSubviews
{
[super layoutSubviews];
// Offset the contentView origin so that it appears correctly w/rt the enclosing scroll view (to which we moved it).
CGRect frame = self.contentView.frame;
frame.origin.x = [self leftUtilityButtonsWidth];
_contentCellView.frame = frame;
self.cellScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.frame) + [self utilityButtonsPadding], CGRectGetHeight(self.frame));
if (!self.cellScrollView.isTracking && !self.cellScrollView.isDecelerating)
{
self.cellScrollView.contentOffset = [self contentOffsetForCellState:_cellState];
}
[self updateCellState];
}
- (void)setFrame:(CGRect)frame
{
layoutUpdating = YES;
// Fix for new screen sizes
// Initially, the cell is still 320 points wide
// We need to layout our subviews again when this changes so our constraints clip to the right width
BOOL widthChanged = (self.frame.size.width != frame.size.width);
[super setFrame:frame];
if (widthChanged)
{
[self layoutIfNeeded];
}
layoutUpdating = NO;
}
- (void)prepareForReuse
{
[super prepareForReuse];
[self hideUtilityButtonsAnimated:NO];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
// Work around stupid background-destroying override magic that UITableView seems to perform on contained buttons.
[self.leftUtilityButtonsView pushBackgroundColors];
[self.rightUtilityButtonsView pushBackgroundColors];
[super setSelected:selected animated:animated];
[self.leftUtilityButtonsView popBackgroundColors];
[self.rightUtilityButtonsView popBackgroundColors];
}
- (void)didTransitionToState:(UITableViewCellStateMask)state {
[super didTransitionToState:state];
if (state == UITableViewCellStateDefaultMask) {
[self layoutSubviews];
}
}
#pragma mark - Selection handling
- (BOOL)shouldHighlight
{
BOOL shouldHighlight = YES;
if ([self.containingTableView.delegate respondsToSelector:@selector(tableView:shouldHighlightRowAtIndexPath:)])
{
NSIndexPath *cellIndexPath = [self.containingTableView indexPathForCell:self];
shouldHighlight = [self.containingTableView.delegate tableView:self.containingTableView shouldHighlightRowAtIndexPath:cellIndexPath];
}
return shouldHighlight;
}
- (void)scrollViewPressed:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan && !self.isHighlighted && self.shouldHighlight)
{
[self setHighlighted:YES animated:NO];
}
else if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
{
// Cell is already highlighted; clearing it temporarily seems to address visual anomaly.
[self setHighlighted:NO animated:NO];
[self scrollViewTapped:gestureRecognizer];
}
else if (gestureRecognizer.state == UIGestureRecognizerStateCancelled)
{
[self setHighlighted:NO animated:NO];
}
}
- (void)scrollViewTapped:(UIGestureRecognizer *)gestureRecognizer
{
if (_cellState == kCellStateCenter)
{
if (self.isSelected)
{
[self deselectCell];
}
else if (self.shouldHighlight) // UITableView refuses selection if highlight is also refused.
{
[self selectCell];
}
}
else
{
// Scroll back to center
[self hideUtilityButtonsAnimated:YES];
}
}
- (void)selectCell
{
if (_cellState == kCellStateCenter)
{
NSIndexPath *cellIndexPath = [self.containingTableView indexPathForCell:self];
if ([self.containingTableView.delegate respondsToSelector:@selector(tableView:willSelectRowAtIndexPath:)])
{
cellIndexPath = [self.containingTableView.delegate tableView:self.containingTableView willSelectRowAtIndexPath:cellIndexPath];
}
if (cellIndexPath)
{
[self.containingTableView selectRowAtIndexPath:cellIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
if ([self.containingTableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)])
{
[self.containingTableView.delegate tableView:self.containingTableView didSelectRowAtIndexPath:cellIndexPath];
}
}
}
}
- (void)deselectCell
{
if (_cellState == kCellStateCenter)
{
NSIndexPath *cellIndexPath = [self.containingTableView indexPathForCell:self];
if ([self.containingTableView.delegate respondsToSelector:@selector(tableView:willDeselectRowAtIndexPath:)])
{
cellIndexPath = [self.containingTableView.delegate tableView:self.containingTableView willDeselectRowAtIndexPath:cellIndexPath];
}
if (cellIndexPath)
{
[self.containingTableView deselectRowAtIndexPath:cellIndexPath animated:NO];
if ([self.containingTableView.delegate respondsToSelector:@selector(tableView:didDeselectRowAtIndexPath:)])
{
[self.containingTableView.delegate tableView:self.containingTableView didDeselectRowAtIndexPath:cellIndexPath];
}
}
}
}
#pragma mark - Utility buttons handling
- (void)rightUtilityButtonHandler:(id)sender
{
SWUtilityButtonTapGestureRecognizer *utilityButtonTapGestureRecognizer = (SWUtilityButtonTapGestureRecognizer *)sender;
NSUInteger utilityButtonIndex = utilityButtonTapGestureRecognizer.buttonIndex;
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCell:didTriggerRightUtilityButtonWithIndex:)])
{
[self.delegate swipeableTableViewCell:self didTriggerRightUtilityButtonWithIndex:utilityButtonIndex];
}
}
- (void)leftUtilityButtonHandler:(id)sender
{
SWUtilityButtonTapGestureRecognizer *utilityButtonTapGestureRecognizer = (SWUtilityButtonTapGestureRecognizer *)sender;
NSUInteger utilityButtonIndex = utilityButtonTapGestureRecognizer.buttonIndex;
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCell:didTriggerLeftUtilityButtonWithIndex:)])
{
[self.delegate swipeableTableViewCell:self didTriggerLeftUtilityButtonWithIndex:utilityButtonIndex];
}
}
- (void)hideUtilityButtonsAnimated:(BOOL)animated
{
if (_cellState != kCellStateCenter)
{
[self.cellScrollView setContentOffset:[self contentOffsetForCellState:kCellStateCenter] animated:animated];
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCell:scrollingToState:)])
{
[self.delegate swipeableTableViewCell:self scrollingToState:kCellStateCenter];
}
}
}
- (void)showLeftUtilityButtonsAnimated:(BOOL)animated {
if (_cellState != kCellStateLeft)
{
[self.cellScrollView setContentOffset:[self contentOffsetForCellState:kCellStateLeft] animated:animated];
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCell:scrollingToState:)])
{
[self.delegate swipeableTableViewCell:self scrollingToState:kCellStateLeft];
}
}
}
- (void)showRightUtilityButtonsAnimated:(BOOL)animated {
if (_cellState != kCellStateRight)
{
[self.cellScrollView setContentOffset:[self contentOffsetForCellState:kCellStateRight] animated:animated];
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCell:scrollingToState:)])
{
[self.delegate swipeableTableViewCell:self scrollingToState:kCellStateRight];
}
}
}
- (BOOL)isUtilityButtonsHidden {
return _cellState == kCellStateCenter;
}
#pragma mark - Geometry helpers
- (CGFloat)leftUtilityButtonsWidth
{
#if CGFLOAT_IS_DOUBLE
return round(CGRectGetWidth(self.leftUtilityButtonsView.frame));
#else
return roundf(CGRectGetWidth(self.leftUtilityButtonsView.frame));
#endif
}
- (CGFloat)rightUtilityButtonsWidth
{
#if CGFLOAT_IS_DOUBLE
return round(CGRectGetWidth(self.rightUtilityButtonsView.frame) + self.additionalRightPadding);
#else
return roundf(CGRectGetWidth(self.rightUtilityButtonsView.frame) + self.additionalRightPadding);
#endif
}
- (CGFloat)utilityButtonsPadding
{
#if CGFLOAT_IS_DOUBLE
return round([self leftUtilityButtonsWidth] + [self rightUtilityButtonsWidth]);
#else
return roundf([self leftUtilityButtonsWidth] + [self rightUtilityButtonsWidth]);
#endif
}
- (CGPoint)contentOffsetForCellState:(SWCellState)state
{
CGPoint scrollPt = CGPointZero;
switch (state)
{
case kCellStateCenter:
scrollPt.x = [self leftUtilityButtonsWidth];
break;
case kCellStateRight:
scrollPt.x = [self utilityButtonsPadding];
break;
case kCellStateLeft:
scrollPt.x = 0;
break;
}
return scrollPt;
}
- (void)updateCellState
{
if(layoutUpdating == NO)
{
// Update the cell state according to the current scroll view contentOffset.
for (NSNumber *numState in @[
@(kCellStateCenter),
@(kCellStateLeft),
@(kCellStateRight),
])
{
SWCellState cellState = numState.integerValue;
if (CGPointEqualToPoint(self.cellScrollView.contentOffset, [self contentOffsetForCellState:cellState]))
{
_cellState = cellState;
break;
}
}
// Update the clipping on the utility button views according to the current position.
CGRect frame = [self.contentView.superview convertRect:self.contentView.frame toView:self];
frame.size.width = CGRectGetWidth(self.frame);
self.leftUtilityClipConstraint.constant = MAX(0, CGRectGetMinX(frame) - CGRectGetMinX(self.frame));
self.rightUtilityClipConstraint.constant = MIN(0, CGRectGetMaxX(frame) - CGRectGetMaxX(self.frame));
if (self.isEditing) {
self.leftUtilityClipConstraint.constant = 0;
self.cellScrollView.contentOffset = CGPointMake([self leftUtilityButtonsWidth], 0);
_cellState = kCellStateCenter;
}
self.leftUtilityClipView.hidden = (self.leftUtilityClipConstraint.constant == 0);
self.rightUtilityClipView.hidden = (self.rightUtilityClipConstraint.constant == 0);
if (self.accessoryType != UITableViewCellAccessoryNone && !self.editing) {
UIView *accessory = [self.cellScrollView.superview.subviews lastObject];
CGRect accessoryFrame = accessory.frame;
accessoryFrame.origin.x = CGRectGetWidth(frame) - CGRectGetWidth(accessoryFrame) - kAccessoryTrailingSpace + CGRectGetMinX(frame);
accessory.frame = accessoryFrame;
}
// Enable or disable the gesture recognizers according to the current mode.
if (!self.cellScrollView.isDragging && !self.cellScrollView.isDecelerating)
{
self.tapGestureRecognizer.enabled = YES;
self.longPressGestureRecognizer.enabled = (_cellState == kCellStateCenter);
}
else
{
self.tapGestureRecognizer.enabled = NO;
self.longPressGestureRecognizer.enabled = NO;
}
self.cellScrollView.scrollEnabled = !self.isEditing;
}
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
if (velocity.x >= 0.5f)
{
if (_cellState == kCellStateLeft || !self.rightUtilityButtons || self.rightUtilityButtonsWidth == 0.0)
{
_cellState = kCellStateCenter;
}
else
{
_cellState = kCellStateRight;
}
}
else if (velocity.x <= -0.5f)
{
if (_cellState == kCellStateRight || !self.leftUtilityButtons || self.leftUtilityButtonsWidth == 0.0)
{
_cellState = kCellStateCenter;
}
else
{
_cellState = kCellStateLeft;
}
}
else
{
CGFloat leftThreshold = [self contentOffsetForCellState:kCellStateLeft].x + (self.leftUtilityButtonsWidth / 2);
CGFloat rightThreshold = [self contentOffsetForCellState:kCellStateRight].x - (self.rightUtilityButtonsWidth / 2);
if (targetContentOffset->x > rightThreshold)
{
_cellState = kCellStateRight;
}
else if (targetContentOffset->x < leftThreshold)
{
_cellState = kCellStateLeft;
}
else
{
_cellState = kCellStateCenter;
}
}
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCell:scrollingToState:)])
{
[self.delegate swipeableTableViewCell:self scrollingToState:_cellState];
}
if (_cellState != kCellStateCenter)
{
if ([self.delegate respondsToSelector:@selector(swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:)])
{
for (SWTableViewCell *cell in [self.containingTableView visibleCells]) {
if (cell != self && [cell isKindOfClass:[SWTableViewCell class]] && [self.delegate swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:cell]) {
[cell hideUtilityButtonsAnimated:YES];
}
}
}
}
*targetContentOffset = [self contentOffsetForCellState:_cellState];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x > [self leftUtilityButtonsWidth])
{
if ([self rightUtilityButtonsWidth] > 0)
{
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableTableViewCell:canSwipeToState:)])
{
BOOL shouldScroll = [self.delegate swipeableTableViewCell:self canSwipeToState:kCellStateRight];
if (!shouldScroll)
{
scrollView.contentOffset = CGPointMake([self leftUtilityButtonsWidth], 0);
}
}
}
else
{
[scrollView setContentOffset:CGPointMake([self leftUtilityButtonsWidth], 0)];
self.tapGestureRecognizer.enabled = YES;
}
}
else
{
// Expose the left button view
if ([self leftUtilityButtonsWidth] > 0)
{
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableTableViewCell:canSwipeToState:)])
{
BOOL shouldScroll = [self.delegate swipeableTableViewCell:self canSwipeToState:kCellStateLeft];
if (!shouldScroll)
{
scrollView.contentOffset = CGPointMake([self leftUtilityButtonsWidth], 0);
}
}
}
else
{
[scrollView setContentOffset:CGPointMake(0, 0)];
self.tapGestureRecognizer.enabled = YES;
}
}
[self updateCellState];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self updateCellState];
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableTableViewCellDidEndScrolling:)]) {
[self.delegate swipeableTableViewCellDidEndScrolling:self];
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
[self updateCellState];
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableTableViewCellDidEndScrolling:)]) {
[self.delegate swipeableTableViewCellDidEndScrolling:self];
}
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (!decelerate)
{
self.tapGestureRecognizer.enabled = YES;
}
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if ((gestureRecognizer == self.containingTableView.panGestureRecognizer && otherGestureRecognizer == self.longPressGestureRecognizer)
|| (gestureRecognizer == self.longPressGestureRecognizer && otherGestureRecognizer == self.containingTableView.panGestureRecognizer))
{
// Return YES so the pan gesture of the containing table view is not cancelled by the long press recognizer
return YES;
}
else
{
return NO;
}
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return ![touch.view isKindOfClass:[UIControl class]];
}
@end

View File

@@ -0,0 +1,17 @@
//
// SWUtilityButtonTapGestureRecognizer.h
// SWTableViewCell
//
// Created by Matt Bowman on 11/27/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
@interface SWUtilityButtonTapGestureRecognizer : UITapGestureRecognizer
@property (nonatomic) NSUInteger buttonIndex;
@end

View File

@@ -0,0 +1,14 @@
//
// SWUtilityButtonTapGestureRecognizer.m
// SWTableViewCell
//
// Created by Matt Bowman on 11/27/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import "SWUtilityButtonTapGestureRecognizer.h"
@implementation SWUtilityButtonTapGestureRecognizer
@end

View File

@@ -0,0 +1,27 @@
//
// SWUtilityButtonView.h
// SWTableViewCell
//
// Created by Matt Bowman on 11/27/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import <UIKit/UIKit.h>
@class SWTableViewCell;
#define kUtilityButtonWidthDefault 90
@interface SWUtilityButtonView : UIView
- (id)initWithUtilityButtons:(NSArray *)utilityButtons parentCell:(SWTableViewCell *)parentCell utilityButtonSelector:(SEL)utilityButtonSelector;
- (id)initWithFrame:(CGRect)frame utilityButtons:(NSArray *)utilityButtons parentCell:(SWTableViewCell *)parentCell utilityButtonSelector:(SEL)utilityButtonSelector;
@property (nonatomic, weak, readonly) SWTableViewCell *parentCell;
@property (nonatomic, copy) NSArray *utilityButtons;
@property (nonatomic, assign) SEL utilityButtonSelector;
- (void)setUtilityButtons:(NSArray *)utilityButtons WithButtonWidth:(CGFloat)width;
- (void)pushBackgroundColors;
- (void)popBackgroundColors;
@end

View File

@@ -0,0 +1,153 @@
//
// SWUtilityButtonView.m
// SWTableViewCell
//
// Created by Matt Bowman on 11/27/13.
// Copyright (c) 2013 Chris Wendel. All rights reserved.
//
#import "SWUtilityButtonView.h"
#import "SWUtilityButtonTapGestureRecognizer.h"
@interface SWUtilityButtonView()
@property (nonatomic, strong) NSLayoutConstraint *widthConstraint;
@property (nonatomic, strong) NSMutableArray *buttonBackgroundColors;
@end
@implementation SWUtilityButtonView
#pragma mark - SWUtilityButonView initializers
- (id)initWithUtilityButtons:(NSArray *)utilityButtons parentCell:(SWTableViewCell *)parentCell utilityButtonSelector:(SEL)utilityButtonSelector
{
self = [self initWithFrame:CGRectZero utilityButtons:utilityButtons parentCell:parentCell utilityButtonSelector:utilityButtonSelector];
return self;
}
- (id)initWithFrame:(CGRect)frame utilityButtons:(NSArray *)utilityButtons parentCell:(SWTableViewCell *)parentCell utilityButtonSelector:(SEL)utilityButtonSelector
{
self = [super initWithFrame:frame];
if (self) {
self.translatesAutoresizingMaskIntoConstraints = NO;
self.widthConstraint = [NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:0.0]; // constant will be adjusted dynamically in -setUtilityButtons:.
self.widthConstraint.priority = UILayoutPriorityDefaultHigh;
[self addConstraint:self.widthConstraint];
_parentCell = parentCell;
self.utilityButtonSelector = utilityButtonSelector;
self.utilityButtons = utilityButtons;
}
return self;
}
#pragma mark Populating utility buttons
- (void)setUtilityButtons:(NSArray *)utilityButtons
{
// if no width specified, use the default width
[self setUtilityButtons:utilityButtons WithButtonWidth:kUtilityButtonWidthDefault];
}
- (void)setUtilityButtons:(NSArray *)utilityButtons WithButtonWidth:(CGFloat)width
{
for (UIButton *button in _utilityButtons)
{
[button removeFromSuperview];
}
_utilityButtons = [utilityButtons copy];
if (utilityButtons.count)
{
NSUInteger utilityButtonsCounter = 0;
UIView *precedingView = nil;
for (UIButton *button in _utilityButtons)
{
[self addSubview:button];
button.translatesAutoresizingMaskIntoConstraints = NO;
if (!precedingView)
{
// First button; pin it to the left edge.
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button]"
options:0L
metrics:nil
views:NSDictionaryOfVariableBindings(button)]];
}
else
{
// Subsequent button; pin it to the right edge of the preceding one, with equal width.
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[precedingView][button(==precedingView)]"
options:0L
metrics:nil
views:NSDictionaryOfVariableBindings(precedingView, button)]];
}
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button]|"
options:0L
metrics:nil
views:NSDictionaryOfVariableBindings(button)]];
SWUtilityButtonTapGestureRecognizer *utilityButtonTapGestureRecognizer = [[SWUtilityButtonTapGestureRecognizer alloc] initWithTarget:_parentCell action:_utilityButtonSelector];
utilityButtonTapGestureRecognizer.buttonIndex = utilityButtonsCounter;
[button addGestureRecognizer:utilityButtonTapGestureRecognizer];
utilityButtonsCounter++;
precedingView = button;
}
// Pin the last button to the right edge.
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[precedingView]|"
options:0L
metrics:nil
views:NSDictionaryOfVariableBindings(precedingView)]];
}
self.widthConstraint.constant = (width * utilityButtons.count);
[self setNeedsLayout];
return;
}
#pragma mark -
- (void)pushBackgroundColors
{
self.buttonBackgroundColors = [[NSMutableArray alloc] init];
for (UIButton *button in self.utilityButtons)
{
[self.buttonBackgroundColors addObject:button.backgroundColor];
}
}
- (void)popBackgroundColors
{
NSEnumerator *e = self.utilityButtons.objectEnumerator;
for (UIColor *color in self.buttonBackgroundColors)
{
UIButton *button = [e nextObject];
button.backgroundColor = color;
}
self.buttonBackgroundColors = nil;
}
@end