diff --git a/WKPagesCollectionView.xcodeproj/project.pbxproj b/WKPagesCollectionView.xcodeproj/project.pbxproj index 644fa51..881f837 100644 --- a/WKPagesCollectionView.xcodeproj/project.pbxproj +++ b/WKPagesCollectionView.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 58C2EC3E1A2F223100583CDE /* WKCloseButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 58C2EC3D1A2F223100583CDE /* WKCloseButton.m */; }; 68A1CF27184CD0DB00346DF8 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 68A1CF26184CD0DB00346DF8 /* README.md */; }; 68BDCE7C18432CCA00DB81A2 /* WKPagesCollectionView.m in Sources */ = {isa = PBXBuildFile; fileRef = 68BDCE7718432CCA00DB81A2 /* WKPagesCollectionView.m */; }; 68BDCE7D18432CCA00DB81A2 /* WKPagesCollectionViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 68BDCE7918432CCA00DB81A2 /* WKPagesCollectionViewCell.m */; }; @@ -24,6 +25,8 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 58C2EC3C1A2F223100583CDE /* WKCloseButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKCloseButton.h; sourceTree = ""; }; + 58C2EC3D1A2F223100583CDE /* WKCloseButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WKCloseButton.m; sourceTree = ""; }; 68A1CF26184CD0DB00346DF8 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = README.md; sourceTree = SOURCE_ROOT; }; 68BDCE7518432CCA00DB81A2 /* WK.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WK.h; sourceTree = ""; }; 68BDCE7618432CCA00DB81A2 /* WKPagesCollectionView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKPagesCollectionView.h; sourceTree = ""; }; @@ -75,6 +78,8 @@ 68BDCE7918432CCA00DB81A2 /* WKPagesCollectionViewCell.m */, 68BDCE7A18432CCA00DB81A2 /* WKPagesCollectionViewFlowLayout.h */, 68BDCE7B18432CCA00DB81A2 /* WKPagesCollectionViewFlowLayout.m */, + 58C2EC3C1A2F223100583CDE /* WKCloseButton.h */, + 58C2EC3D1A2F223100583CDE /* WKCloseButton.m */, ); path = WKPagesCollectionView; sourceTree = ""; @@ -205,6 +210,7 @@ 68D7C1E918347A08006B418C /* ViewController.m in Sources */, 68D7C1E318347A08006B418C /* AppDelegate.m in Sources */, 68BDCE7C18432CCA00DB81A2 /* WKPagesCollectionView.m in Sources */, + 58C2EC3E1A2F223100583CDE /* WKCloseButton.m in Sources */, 68D7C1DF18347A08006B418C /* main.m in Sources */, 68BDCE7E18432CCA00DB81A2 /* WKPagesCollectionViewFlowLayout.m in Sources */, 68BDCE7D18432CCA00DB81A2 /* WKPagesCollectionViewCell.m in Sources */, diff --git a/WKPagesScrollView/WKPagesCollectionView/WKCloseButton.h b/WKPagesScrollView/WKPagesCollectionView/WKCloseButton.h new file mode 100644 index 0000000..8051441 --- /dev/null +++ b/WKPagesScrollView/WKPagesCollectionView/WKCloseButton.h @@ -0,0 +1,13 @@ +// +// WKCloseButton.h +// WKPagesCollectionView +// +// Created by Xu Zhao on 3/12/14. +// Copyright (c) 2014 秦 道平. All rights reserved. +// + +#import + +@interface WKCloseButton : UIButton + +@end diff --git a/WKPagesScrollView/WKPagesCollectionView/WKCloseButton.m b/WKPagesScrollView/WKPagesCollectionView/WKCloseButton.m new file mode 100644 index 0000000..d0bf50f --- /dev/null +++ b/WKPagesScrollView/WKPagesCollectionView/WKCloseButton.m @@ -0,0 +1,45 @@ +// +// WKCloseButton.m +// WKPagesCollectionView +// +// Created by Xu Zhao on 3/12/14. +// Copyright (c) 2014 秦 道平. All rights reserved. +// + +#import "WKCloseButton.h" +#import +@implementation WKCloseButton + + +-(id) initWithFrame:(CGRect)frame +{ + self = [super initWithFrame:frame]; + if (self){ + [self drawCloseButton]; + } + return self; + +} +#pragma mark - DrawButton +-(void) drawCloseButton +{ + self.backgroundColor = [UIColor clearColor]; + CGFloat xOffset = self.bounds.size.width / 7; + CGFloat yOffset = self.bounds.size.width / 7; + CGFloat width = self.frame.size.width * 0.3; + CGFloat height = self.frame.size.height * 0.3; + UIBezierPath *path = [UIBezierPath bezierPath]; + [path moveToPoint:CGPointMake(xOffset, yOffset)]; + [path addLineToPoint:CGPointMake(width + xOffset, height + yOffset)]; + [path moveToPoint:CGPointMake(width + xOffset, yOffset)]; + [path addLineToPoint:CGPointMake(xOffset, height + yOffset)]; + + CAShapeLayer *shapeLayer = [CAShapeLayer layer]; + [shapeLayer setPath:[path CGPath]]; + [shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]]; + [shapeLayer setLineWidth:1.0f]; + + [[self layer] addSublayer:shapeLayer]; +} + +@end diff --git a/WKPagesScrollView/WKPagesCollectionView/WKPagesCollectionViewCell.h b/WKPagesScrollView/WKPagesCollectionView/WKPagesCollectionViewCell.h index 22db0d1..8adf184 100644 --- a/WKPagesScrollView/WKPagesCollectionView/WKPagesCollectionViewCell.h +++ b/WKPagesScrollView/WKPagesCollectionView/WKPagesCollectionViewCell.h @@ -7,6 +7,7 @@ // #import +#import "WKCloseButton.h" typedef enum WKPagesCollectionViewCellShowingState:NSUInteger{ WKPagesCollectionViewCellShowingStateNormal=0, WKPagesCollectionViewCellShowingStateHightlight=1, @@ -17,6 +18,7 @@ typedef enum WKPagesCollectionViewCellShowingState:NSUInteger{ WKPagesCollectionViewCellShowingState _showingState; UITapGestureRecognizer* _tapGesture; UIScrollView* _scrollView; + WKCloseButton* _closeButton; // UIImageView* _maskImageView; } ///Position the normal state diff --git a/WKPagesScrollView/WKPagesCollectionView/WKPagesCollectionViewCell.m b/WKPagesScrollView/WKPagesCollectionView/WKPagesCollectionViewCell.m index 6fd66ef..668f8dc 100644 --- a/WKPagesScrollView/WKPagesCollectionView/WKPagesCollectionViewCell.m +++ b/WKPagesScrollView/WKPagesCollectionView/WKPagesCollectionViewCell.m @@ -8,6 +8,9 @@ #import "WKPagesCollectionViewCell.h" #import "WKPagesCollectionView.h" +#import "WKCloseButton.h" +#define CloseButtonWidth 35 +#define CloseButtonHeight CloseButtonWidth * 1.5 @implementation WKPagesCollectionViewCell @dynamic showingState; - (id)initWithFrame:(CGRect)frame @@ -44,6 +47,13 @@ - (id)initWithFrame:(CGRect)frame _tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTapGesture:)]; [_scrollView addGestureRecognizer:_tapGesture]; } + + if (!_closeButton) { + _closeButton = [[WKCloseButton alloc] initWithFrame:CGRectMake(0, 0, CloseButtonWidth, CloseButtonHeight)]; + [_closeButton addTarget:self action:@selector(closeButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; + [_scrollView addSubview:_closeButton]; + } + } return self; } @@ -77,6 +87,12 @@ -(IBAction)onTapGesture:(UITapGestureRecognizer*)tapGesture{ NSLog(@"highlight completed"); }]; } + +-(void) closeButtonPressed:(id)sender +{ + [self removeCurrentCell]; + +} #pragma mark - Properties -(void)setShowingState:(WKPagesCollectionViewCellShowingState)showingState{ if (_showingState==showingState) @@ -89,6 +105,7 @@ -(void)setShowingState:(WKPagesCollectionViewCellShowingState)showingState{ case WKPagesCollectionViewCellShowingStateHightlight:{ self.normalTransform=self.layer.transform;///The original location of the first record _scrollView.scrollEnabled=NO; + _closeButton.hidden = YES; NSIndexPath* indexPath=[self.collectionView indexPathForCell:self]; CGFloat moveY=self.collectionView.contentOffset.y-(WKPagesCollectionViewPageSpacing)*indexPath.row +topMargin; CATransform3D moveTransform=CATransform3DMakeTranslation(0.0f, moveY, 0.0f); @@ -98,6 +115,7 @@ -(void)setShowingState:(WKPagesCollectionViewCellShowingState)showingState{ case WKPagesCollectionViewCellShowingStateBackToTop:{ self.normalTransform=self.layer.transform;///The original location of the first record _scrollView.scrollEnabled=NO; + _closeButton.hidden = NO; CATransform3D moveTransform=CATransform3DMakeTranslation(0, -1*pageHeight-topMargin, 0); self.layer.transform=CATransform3DConcat(CATransform3DIdentity, moveTransform); } @@ -105,6 +123,7 @@ -(void)setShowingState:(WKPagesCollectionViewCellShowingState)showingState{ case WKPagesCollectionViewCellShowingStateBackToBottom:{ self.normalTransform=self.layer.transform;///The original location of the first record _scrollView.scrollEnabled=NO; + _closeButton.hidden = NO; CATransform3D moveTransform=CATransform3DMakeTranslation(0, pageHeight+topMargin, 0); self.layer.transform=CATransform3DConcat(CATransform3DIdentity, moveTransform); } @@ -112,6 +131,7 @@ -(void)setShowingState:(WKPagesCollectionViewCellShowingState)showingState{ case WKPagesCollectionViewCellShowingStateNormal:{ self.layer.transform=self.normalTransform; _scrollView.scrollEnabled=YES; + _closeButton.hidden = NO; } break; default: @@ -124,6 +144,25 @@ -(void)setShowingState:(WKPagesCollectionViewCellShowingState)showingState{ -(WKPagesCollectionViewCellShowingState)showingState{ return _showingState; } + + +-(void) removeCurrentCell +{ + NSIndexPath* indexPath=[self.collectionView indexPathForCell:self]; + NSLog(@"delete cell at %ld",(long)indexPath.row); + //self.alpha=0.0f; + //Delete data + id pagesDataSource=(id)self.collectionView.dataSource; + [pagesDataSource collectionView:(WKPagesCollectionView*)self.collectionView willRemoveCellAtIndexPath:indexPath]; + //Animation + [self.collectionView performBatchUpdates:^{ + [self.collectionView deleteItemsAtIndexPaths:@[indexPath,]]; + } completion:^(BOOL finished) { + + }]; + +} + #pragma mark - UIScrollViewDelegate -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ @@ -135,18 +174,7 @@ -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL) if (self.showingState==WKPagesCollectionViewCellShowingStateNormal){ CGFloat slideDistance = scrollView.frame.size.width / 6; if (scrollView.contentOffset.x >= slideDistance){ - NSIndexPath* indexPath=[self.collectionView indexPathForCell:self]; - NSLog(@"delete cell at %ld",(long)indexPath.row); - //self.alpha=0.0f; - ///Delete data - id pagesDataSource=(id)self.collectionView.dataSource; - [pagesDataSource collectionView:(WKPagesCollectionView*)self.collectionView willRemoveCellAtIndexPath:indexPath]; - ///Animation - [self.collectionView performBatchUpdates:^{ - [self.collectionView deleteItemsAtIndexPaths:@[indexPath,]]; - } completion:^(BOOL finished) { - - }]; + [self removeCurrentCell]; } } }