Skip to content

fix item slip on backgroundMask butween scrollView and Frame #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 43 additions & 28 deletions AWIconSheet/AWActionSheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ @interface AWActionSheet()<UIScrollViewDelegate>

@property (nonatomic, strong) UIView *backgroundMask;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, assign) BOOL showCancelButton;
@end
@implementation AWActionSheet
@synthesize scrollView;
@synthesize pageControl;
@synthesize items;
@synthesize IconDelegate;

static float scrollviewToFrame = 10.0f;

-(void)dealloc
{
IconDelegate= nil;
Expand All @@ -37,6 +41,7 @@ -(id)initWithIconSheetDelegate:(id<AWActionSheetDelegate>)delegate ItemCount:(in
if (self) {
self.backgroundColor = [UIColor clearColor];
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.showCancelButton = YES;

self.backgroundMask = [[UIView alloc] initWithFrame:self.bounds];
self.backgroundMask.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
Expand All @@ -53,7 +58,7 @@ -(id)initWithIconSheetDelegate:(id<AWActionSheetDelegate>)delegate ItemCount:(in
self.itemCountforOneLine = 4;

IconDelegate = delegate;
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10, CGRectGetWidth(self.contentView.bounds), 105*3)];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(scrollviewToFrame, 10, CGRectGetWidth(self.contentView.bounds) - scrollviewToFrame * 2, 105*3)];
[scrollView setPagingEnabled:YES];
[scrollView setBackgroundColor:[UIColor clearColor]];
[scrollView setShowsHorizontalScrollIndicator:NO];
Expand All @@ -64,7 +69,7 @@ -(id)initWithIconSheetDelegate:(id<AWActionSheetDelegate>)delegate ItemCount:(in

[self.contentView addSubview:scrollView];

self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.scrollView.frame), CGRectGetWidth(self.contentView.bounds), 20)];
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.scrollView.frame), CGRectGetWidth(self.scrollView.bounds), 20)];
[pageControl setNumberOfPages:0];
[pageControl setCurrentPage:0];
pageControl.hidesForSinglePage = YES;
Expand Down Expand Up @@ -128,20 +133,21 @@ - (void)reloadData
int rowCount = 3;

if (count <= self.itemCountforOneLine) {
[scrollView setFrame:CGRectMake(0, 10, CGRectGetWidth(self.contentView.bounds), 105)];
[scrollView setFrame:CGRectMake(scrollviewToFrame, 10, CGRectGetWidth(self.contentView.bounds) - scrollviewToFrame * 2, 105)];
rowCount = 1;
} else if (count <= self.itemCountforOneLine*2) {
[scrollView setFrame:CGRectMake(0, 10, CGRectGetWidth(self.contentView.bounds), 210)];
[scrollView setFrame:CGRectMake(scrollviewToFrame, 10, CGRectGetWidth(self.contentView.bounds) - scrollviewToFrame * 2, 210)];
rowCount = 2;
}
self.scrollView.backgroundColor = [UIColor whiteColor];

CGFloat pageControlY = CGRectGetMinY(self.scrollView.frame) + CGRectGetHeight(self.scrollView.frame);
CGRect pageControlFrame = self.pageControl.frame;
pageControlFrame.origin.y = pageControlY;
self.pageControl.frame = pageControlFrame;

NSUInteger itemPerPage = self.itemCountforOneLine*rowCount;
[scrollView setContentSize:CGSizeMake(CGRectGetWidth(self.contentView.bounds)*ceilf((((float)count)/itemPerPage)), scrollView.frame.size.height)];
[scrollView setContentSize:CGSizeMake(CGRectGetWidth(self.scrollView.bounds)*ceilf((((float)count)/itemPerPage)), scrollView.frame.size.height)];
[pageControl setNumberOfPages:ceilf((((float)count)/itemPerPage))];
[pageControl setCurrentPage:0];

Expand All @@ -159,38 +165,47 @@ - (void)reloadData
float centerY = (1+row*2)*self.scrollView.frame.size.height/(2*rowCount);
float centerX = (1+column*2)*width/(2*self.itemCountforOneLine);

[cell setCenter:CGPointMake(margin + centerX+CGRectGetWidth(self.contentView.bounds)*PageNo, centerY)];
[cell setCenter:CGPointMake(margin + centerX+CGRectGetWidth(self.scrollView.bounds)*PageNo, centerY)];
[self.scrollView addSubview:cell];

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionForItem:)];
[cell addGestureRecognizer:tap];

[items addObject:cell];
}

UIView *scrollBG = [[UIView alloc] initWithFrame:CGRectMake(margin, 8, CGRectGetWidth(self.scrollView.frame) - margin * 2, CGRectGetHeight(self.scrollView.frame))];
scrollBG.backgroundColor = [UIColor whiteColor];
scrollBG.alpha = 0.9;
scrollBG.clipsToBounds = YES;
scrollBG.layer.cornerRadius = 5;
[self.contentView insertSubview:scrollBG belowSubview:self.scrollView];
// UIView *scrollBG = [[UIView alloc] initWithFrame:CGRectMake(margin, 8, CGRectGetWidth(self.scrollView.frame) - margin * 2, CGRectGetHeight(self.scrollView.frame))];
// scrollBG.backgroundColor = [UIColor whiteColor];
// scrollBG.alpha = 0.9;
// scrollBG.clipsToBounds = YES;
// scrollBG.layer.cornerRadius = 5;
// [self.contentView insertSubview:scrollBG belowSubview:self.scrollView];

CGFloat y = CGRectGetMinY(self.pageControl.frame) + 5 + (self.pageControl.numberOfPages == 1 ? 0 : CGRectGetHeight(self.pageControl.frame));
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(margin, y, CGRectGetWidth(self.contentView.frame) - margin * 2, 44);
[btn setTitle:@"取消" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor colorWithRed:0.22 green:0.45 blue:1 alpha:1] forState:UIControlStateNormal];
[self setBtn:btn backgroundColor:[UIColor colorWithWhite:1 alpha:0.9]];
btn.layer.cornerRadius = 5;
btn.clipsToBounds = YES;
[btn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:btn];

CGFloat height = CGRectGetMinY(btn.frame) + CGRectGetHeight(btn.frame) + 10;
if (self.showCancelButton) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(margin, y, CGRectGetWidth(self.contentView.frame) - margin * 2, 44);
[btn setTitle:@"取消" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor colorWithRed:0.22 green:0.45 blue:1 alpha:1] forState:UIControlStateNormal];
[self setBtn:btn backgroundColor:[UIColor colorWithWhite:1 alpha:0.9]];
btn.layer.cornerRadius = 5;
btn.clipsToBounds = YES;
[btn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:btn];

CGFloat height = CGRectGetMinY(btn.frame) + CGRectGetHeight(btn.frame) + 10;

CGRect frame = self.contentView.frame;
frame.size.height = height;
self.contentView.frame = frame;
}
else
{
CGRect frame = self.contentView.frame;
frame.size.height = y;
self.contentView.frame = frame;
}

CGRect frame = self.contentView.frame;
frame.size.height = height;
self.contentView.frame = frame;
}

- (void)setBtn:(UIButton*)btn backgroundColor:(UIColor*)color
Expand Down Expand Up @@ -221,13 +236,13 @@ - (void)actionForItem:(UITapGestureRecognizer*)recongizer

- (IBAction)changePage:(id)sender {
int page = (int)pageControl.currentPage;
[scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.contentView.bounds) * page, 0)];
[scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.scrollView.bounds) * page, 0)];
}
#pragma mark -
#pragma scrollview delegate

- (void)scrollViewDidScroll:(UIScrollView *)sender {
int page = scrollView.contentOffset.x /CGRectGetWidth(self.contentView.bounds);
int page = scrollView.contentOffset.x /CGRectGetWidth(self.scrollView.bounds);
pageControl.currentPage = page;
}

Expand Down