Skip to content

Commit

Permalink
修复在iOS10时由于系统做了优化导致的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
idhong committed Jul 23, 2017
1 parent 7b0eea5 commit a2fb9a6
Show file tree
Hide file tree
Showing 16 changed files with 397 additions and 33 deletions.
2 changes: 1 addition & 1 deletion BMDragCellCollectionView.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'BMDragCellCollectionView'
s.version = '1.0.1'
s.version = '1.0.2'
s.summary = '一款可以简单实现长按拖拽重排的 UICellCollectionView Cell框架,简单实现支付宝等拖拽重排功能,完美支持iOS6+'
s.homepage = 'https://github.com/asiosldh/BMDragCellCollectionView'
s.license = 'MIT'
Expand Down
53 changes: 30 additions & 23 deletions BMDragCellCollectionView/BMDragCellCollectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ typedef NS_ENUM(NSUInteger, BMDragCellCollectionViewScrollDirection) {

@interface BMDragCellCollectionView ()

@property (strong, nonatomic) UILongPressGestureRecognizer *longGesture; ///< 长按手势
@property (strong, nonatomic) UIView *snapedView; ///< 截图快照
@property (strong, nonatomic) CADisplayLink *edgeTimer; ///< 定时器
@property (strong, nonatomic) NSIndexPath *currentIndexPath; ///< 当前路径
@property (strong, nonatomic) NSIndexPath *oldIndexPath; ///< 旧路径
@property (strong, nonatomic) NSIndexPath *oldIndexPath; ///< 旧的IndexPath
@property (nonatomic, assign) CGPoint oldPoint; ///< 旧的位置
@property (assign, nonatomic) CGPoint lastPoint; ///< 最后的触摸点
@property (assign, nonatomic) BOOL isEndDrag; ///< 是否正在拖动
@property (strong, nonatomic) UILongPressGestureRecognizer *longGesture; ///< 长按手势

@end

Expand Down Expand Up @@ -117,6 +118,9 @@ - (void)initConfiguration {
_canDrag = YES;
_minimumPressDuration = .5f;
[self addGestureRecognizer:self.longGesture];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
self.prefetchingEnabled = NO;
}
}

- (UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath {
Expand Down Expand Up @@ -218,6 +222,7 @@ - (void)_stopEdgeTimer{
}

- (void)_edgeScroll {

BMDragCellCollectionViewScrollDirection scrollDirection = [self _setScrollDirection];
switch (scrollDirection) {
case BMDragCellCollectionViewScrollDirectionLeft:{
Expand Down Expand Up @@ -261,10 +266,14 @@ - (void)_edgeScroll {
if (!index) {
return;
}

NSIndexPath *indexPath = index;
_currentIndexPath = indexPath;


if (self.delegate && [self.delegate respondsToSelector:@selector(dragCellCollectionViewShouldBeginExchange:sourceIndexPath:toIndexPath:)]) {
if (![self.delegate dragCellCollectionViewShouldBeginExchange:self sourceIndexPath:_oldIndexPath toIndexPath:index]) {
return;
}
}
_currentIndexPath = index;
self.oldPoint = [self cellForItemAtIndexPath:_currentIndexPath].center;
[self _updateSourceData];

// 移动 会调用willMoveToIndexPath方法更新数据源
Expand All @@ -285,6 +294,12 @@ - (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
_oldIndexPath = [self indexPathForItemAtPoint:[longGesture locationInView:self]];
// 没有按在cell 上就 break
if (_oldIndexPath == nil) {
self.longGesture.enabled = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (_canDrag) {
self.longGesture.enabled = YES;
}
});
break;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(dragCellCollectionViewShouldBeginMove:indexPath:)]) {
Expand All @@ -302,7 +317,8 @@ - (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
self.isEndDrag = NO;
// 取出正在长按的cell
UICollectionViewCell *cell = [self cellForItemAtIndexPath:_oldIndexPath];

self.oldPoint = cell.center;

// 使用系统截图功能,得到cell的快照view
_snapedView = [cell snapshotViewAfterScreenUpdates:NO];
// 设置frame
Expand All @@ -325,7 +341,7 @@ - (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
[self _setEdgeTimer];
}
break;
case UIGestureRecognizerStateChanged:{
case UIGestureRecognizerStateChanged: {
// 当前手指位置
_lastPoint = [longGesture locationInView:self];
// 截图视图位置移动
Expand All @@ -347,7 +363,8 @@ - (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
}

_currentIndexPath = index;

self.oldPoint = [self cellForItemAtIndexPath:_currentIndexPath].center;

[self _updateSourceData];

// 移动 会调用willMoveToIndexPath方法更新数据源
Expand All @@ -362,23 +379,14 @@ - (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
return;
}
UICollectionViewCell *cell = [self cellForItemAtIndexPath:_oldIndexPath];

//结束动画过程中停止交互,防止出问题
self.userInteractionEnabled = NO;

__block BOOL sel = NO;
[[self indexPathsForVisibleItems] enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.item == self.oldIndexPath.item && obj.section == self.oldIndexPath.section) {
sel = YES;
}
}];
self.isEndDrag = YES;
//给截图视图一个动画移动到隐藏cell的新位置
[UIView animateWithDuration:0.25 animations:^{
if (sel) {
_snapedView.center = cell.center;
_snapedView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
} else {
_snapedView.alpha = 0;
}
_snapedView.center = self.oldPoint;
_snapedView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
} completion:^(BOOL finished) {
//移除截图视图、显示隐藏的cell并开启交互
[_snapedView removeFromSuperview];
Expand All @@ -389,7 +397,6 @@ - (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
}
}];
// 关闭定时器
self.isEndDrag = YES;
self.oldIndexPath = nil;
[self _stopEdgeTimer];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
objects = {

/* Begin PBXBuildFile section */
3139EB581F250000007762D8 /* BMTodayHeadlinesDragCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 3139EB521F250000007762D8 /* BMTodayHeadlinesDragCell.m */; };
3139EB591F250000007762D8 /* BMTodayHeadlinesDragCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3139EB531F250000007762D8 /* BMTodayHeadlinesDragCell.xib */; };
3139EB5A1F250000007762D8 /* BMTodayHeadlinesDragModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 3139EB551F250000007762D8 /* BMTodayHeadlinesDragModel.m */; };
3139EB601F25006C007762D8 /* BMTodayHeadlinesDragVC.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3139EB5E1F25006C007762D8 /* BMTodayHeadlinesDragVC.xib */; };
3139EB631F2500B6007762D8 /* BMTodayHeadlinesDragVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 3139EB621F2500B6007762D8 /* BMTodayHeadlinesDragVC.m */; };
31ADB06A1F1D04EB00FE7FA1 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 31ADB0681F1D04EB00FE7FA1 /* Main.storyboard */; };
31ADB06C1F1D04EB00FE7FA1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 31ADB06B1F1D04EB00FE7FA1 /* Assets.xcassets */; };
31ADB06F1F1D04EB00FE7FA1 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 31ADB06D1F1D04EB00FE7FA1 /* LaunchScreen.storyboard */; };
Expand Down Expand Up @@ -42,6 +47,14 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
3139EB511F250000007762D8 /* BMTodayHeadlinesDragCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BMTodayHeadlinesDragCell.h; sourceTree = "<group>"; };
3139EB521F250000007762D8 /* BMTodayHeadlinesDragCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BMTodayHeadlinesDragCell.m; sourceTree = "<group>"; };
3139EB531F250000007762D8 /* BMTodayHeadlinesDragCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = BMTodayHeadlinesDragCell.xib; sourceTree = "<group>"; };
3139EB541F250000007762D8 /* BMTodayHeadlinesDragModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BMTodayHeadlinesDragModel.h; sourceTree = "<group>"; };
3139EB551F250000007762D8 /* BMTodayHeadlinesDragModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BMTodayHeadlinesDragModel.m; sourceTree = "<group>"; };
3139EB5E1F25006C007762D8 /* BMTodayHeadlinesDragVC.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = BMTodayHeadlinesDragVC.xib; sourceTree = "<group>"; };
3139EB611F2500B6007762D8 /* BMTodayHeadlinesDragVC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BMTodayHeadlinesDragVC.h; sourceTree = "<group>"; };
3139EB621F2500B6007762D8 /* BMTodayHeadlinesDragVC.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BMTodayHeadlinesDragVC.m; sourceTree = "<group>"; };
31ADB05C1F1D04EB00FE7FA1 /* BMDragCellCollectionViewDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BMDragCellCollectionViewDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
31ADB0691F1D04EB00FE7FA1 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
31ADB06B1F1D04EB00FE7FA1 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -95,6 +108,21 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
3139EB501F250000007762D8 /* BMTodayHeadlinesDrag */ = {
isa = PBXGroup;
children = (
3139EB611F2500B6007762D8 /* BMTodayHeadlinesDragVC.h */,
3139EB621F2500B6007762D8 /* BMTodayHeadlinesDragVC.m */,
3139EB5E1F25006C007762D8 /* BMTodayHeadlinesDragVC.xib */,
3139EB511F250000007762D8 /* BMTodayHeadlinesDragCell.h */,
3139EB521F250000007762D8 /* BMTodayHeadlinesDragCell.m */,
3139EB531F250000007762D8 /* BMTodayHeadlinesDragCell.xib */,
3139EB541F250000007762D8 /* BMTodayHeadlinesDragModel.h */,
3139EB551F250000007762D8 /* BMTodayHeadlinesDragModel.m */,
);
path = BMTodayHeadlinesDrag;
sourceTree = "<group>";
};
31ADB0531F1D04EB00FE7FA1 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -171,6 +199,7 @@
31B00B2B1F20F3D500A8670B /* Demo */ = {
isa = PBXGroup;
children = (
3139EB501F250000007762D8 /* BMTodayHeadlinesDrag */,
31B00B6B1F233C5D00A8670B /* BMDragCollectionViewCell.h */,
31B00B6C1F233C5D00A8670B /* BMDragCollectionViewCell.m */,
31B00B6D1F233C5D00A8670B /* BMDragCollectionViewCell.xib */,
Expand Down Expand Up @@ -307,6 +336,8 @@
31ADB06C1F1D04EB00FE7FA1 /* Assets.xcassets in Resources */,
31B00B731F238DAC00A8670B /* BMDragCellCollectionView.podspec in Resources */,
31ADB06A1F1D04EB00FE7FA1 /* Main.storyboard in Resources */,
3139EB591F250000007762D8 /* BMTodayHeadlinesDragCell.xib in Resources */,
3139EB601F25006C007762D8 /* BMTodayHeadlinesDragVC.xib in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -331,12 +362,15 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3139EB581F250000007762D8 /* BMTodayHeadlinesDragCell.m in Sources */,
31B00B641F22507C00A8670B /* BMDragCellCollectionView.m in Sources */,
31B00B331F20F3D500A8670B /* AppDelegate.m in Sources */,
31B00B6E1F233C5D00A8670B /* BMDragCollectionViewCell.m in Sources */,
31B00B751F238DAC00A8670B /* README.md in Sources */,
31B00B6A1F2252C500A8670B /* ViewController.m in Sources */,
31B00B671F22524900A8670B /* BMDragCellCollectionViewVC.m in Sources */,
3139EB631F2500B6007762D8 /* BMTodayHeadlinesDragVC.m in Sources */,
3139EB5A1F250000007762D8 /* BMTodayHeadlinesDragModel.m in Sources */,
31B00B341F20F3D500A8670B /* main.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<rect key="frame" x="20" y="240" width="335" height="30"/>
<color key="backgroundColor" red="0.84313725490000002" green="0.84313725490000002" blue="0.84313725490000002" alpha="1" colorSpace="calibratedRGB"/>
<state key="normal" title="其他一些模仿例子正在完成中..."/>
<connections>
<action selector="otherDemoClick" destination="BYZ-38-t0r" eventType="touchUpInside" id="kee-q0-qTx"/>
</connections>
</button>
</subviews>
<constraints>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#import "BMDragCollectionViewCell.h"
#import "BMDragCellCollectionView.h"

#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height
#define WIDTH [[UIScreen mainScreen] bounds].size.width
#define HEIGHT [[UIScreen mainScreen] bounds].size.height

@interface BMDragCellCollectionViewVC () <BMDragCellCollectionViewDelegate, BMDragCollectionViewDataSource>
@interface BMDragCellCollectionViewVC () <UICollectionViewDelegateFlowLayout,BMDragCellCollectionViewDelegate, BMDragCollectionViewDataSource>

@property (nonatomic, strong) BMDragCellCollectionView *collectionView; // collectionView
@property (strong, nonatomic) NSMutableArray *dataSourceArray;
Expand All @@ -31,7 +31,6 @@ - (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.collectionView];


UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
BMDragCellCollectionView *collectionView = [[BMDragCellCollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:layout];
collectionView.delegate = self;
Expand Down Expand Up @@ -93,8 +92,4 @@ - (void)dragCellCollectionView:(BMDragCellCollectionView *)dragCellCollectionVie
self.dataSource = [newDataArray mutableCopy];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return [self.dataSource[indexPath.section][indexPath.item][@"size"] CGSizeValue];
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// BMTodayHeadlinesDragCell.h
// TodayHeadlinesDrag
//
// Created by __liangdahong on 2017/7/23.
// Copyright © 2017年 http://idhong.com. All rights reserved.
//

#import <UIKit/UIKit.h>

@class BMTodayHeadlinesDragModel;

@interface BMTodayHeadlinesDragCell : UICollectionViewCell

@property (strong, nonatomic) BMTodayHeadlinesDragModel *todayHeadlinesDragModel;


@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

//
// BMTodayHeadlinesDragCell.m
// TodayHeadlinesDrag
//
// Created by __liangdahong on 2017/7/23.
// Copyright © 2017年 http://idhong.com. All rights reserved.
//

#import "BMTodayHeadlinesDragCell.h"
#import "BMTodayHeadlinesDragModel.h"

@interface BMTodayHeadlinesDragCell ()

@property (weak, nonatomic) IBOutlet UIView *mybackgroundView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@end

@implementation BMTodayHeadlinesDragCell

- (void)awakeFromNib {
[super awakeFromNib];
self.titleLabel.adjustsFontSizeToFitWidth = YES;
self.titleLabel.font = [UIFont systemFontOfSize:13];
self.mybackgroundView.layer.cornerRadius = 8.0;
self.mybackgroundView.layer.borderColor = [UIColor grayColor].CGColor;
self.mybackgroundView.layer.borderWidth = 1.0;
}

- (void)setTodayHeadlinesDragModel:(BMTodayHeadlinesDragModel *)todayHeadlinesDragModel {
_todayHeadlinesDragModel = todayHeadlinesDragModel;
self.titleLabel.text = todayHeadlinesDragModel.title;
}

@end
Loading

0 comments on commit a2fb9a6

Please sign in to comment.