diff --git a/PYSearch/NSBundle+PYSearchExtension.h b/PYSearch/NSBundle+PYSearchExtension.h index ad3b139..b76d073 100644 --- a/PYSearch/NSBundle+PYSearchExtension.h +++ b/PYSearch/NSBundle+PYSearchExtension.h @@ -15,7 +15,8 @@ /** 获取PYSearch.bundle路径 */ + (NSBundle *)py_searchBundle; - -+ (UIImage*)py_imageNamed:(NSString*)name; + +/** 获取PYSearch.bundle路径中的图片 */ ++ (UIImage *)py_imageNamed:(NSString *)name; @end diff --git a/PYSearch/NSBundle+PYSearchExtension.m b/PYSearch/NSBundle+PYSearchExtension.m index 776524d..d440b2b 100644 --- a/PYSearch/NSBundle+PYSearchExtension.m +++ b/PYSearch/NSBundle+PYSearchExtension.m @@ -56,11 +56,9 @@ + (NSString *)py_localizedStringForKey:(NSString *)key value:(NSString *)value return [[NSBundle mainBundle] localizedStringForKey:key value:value table:nil]; } -+ (UIImage*)py_imageNamed:(NSString*)name ++ (UIImage *)py_imageNamed:(NSString *)name { - UIImage* image = [UIImage imageWithContentsOfFile: [[[NSBundle py_searchBundle] resourcePath] stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.png", name]]]; - - return image; + return [UIImage imageWithContentsOfFile:[[[NSBundle py_searchBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", name]]]; } @end diff --git a/PYSearch/PYSearchSuggestionViewController.h b/PYSearch/PYSearchSuggestionViewController.h index df45f43..99e442a 100644 --- a/PYSearch/PYSearchSuggestionViewController.h +++ b/PYSearch/PYSearchSuggestionViewController.h @@ -25,6 +25,7 @@ typedef void(^PYSearchSuggestionDidSelectCellBlock)(UITableViewCell *selectedCel @end @interface PYSearchSuggestionViewController : UITableViewController + /** 搜索建议数据源 */ @property (nonatomic, weak) id dataSource; /** 搜索建议 */ diff --git a/PYSearch/PYSearchSuggestionViewController.m b/PYSearch/PYSearchSuggestionViewController.m index 8091b11..2c23b20 100644 --- a/PYSearch/PYSearchSuggestionViewController.m +++ b/PYSearch/PYSearchSuggestionViewController.m @@ -9,6 +9,7 @@ #import "PYSearchConst.h" @interface PYSearchSuggestionViewController () + /** 记录消失前的contentInset */ @property (nonatomic, assign) UIEdgeInsets originalContentInset; @@ -74,7 +75,6 @@ - (void)setSearchSuggestions:(NSArray *)searchSuggestions self.tableView.contentOffset = CGPointMake(0, -self.tableView.contentInset.top); } - #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if ([self.dataSource respondsToSelector:@selector(numberOfSectionsInSearchSuggestionView:)]) { @@ -105,7 +105,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N cell.textLabel.font = [UIFont systemFontOfSize:14]; cell.backgroundColor = [UIColor clearColor]; // 添加分割线 - UIImageView *line = [[UIImageView alloc] initWithImage: [NSBundle py_imageNamed: @"cell-content-line"]]; + UIImageView *line = [[UIImageView alloc] initWithImage: [NSBundle py_imageNamed:@"cell-content-line"]]; line.py_height = 0.5; line.alpha = 0.7; line.py_x = PYSEARCH_MARGIN; @@ -114,7 +114,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N [cell.contentView addSubview:line]; } // 设置数据 - cell.imageView.image = [NSBundle py_imageNamed: @"search"]; + cell.imageView.image = [NSBundle py_imageNamed:@"search"]; cell.textLabel.text = self.searchSuggestions[indexPath.row]; return cell; } diff --git a/PYSearch/PYSearchViewController.m b/PYSearch/PYSearchViewController.m index 8c3c912..ebcdb43 100644 --- a/PYSearch/PYSearchViewController.m +++ b/PYSearch/PYSearchViewController.m @@ -95,7 +95,9 @@ - (void)viewWillAppear:(BOOL)animated if (self.navigationController.navigationBar.translucent == NO) { self.baseSearchTableView.contentInset = UIEdgeInsetsMake(0, 0, self.view.py_y, 0); self.searchSuggestionVC.view.frame = CGRectMake(0, 64 - self.view.py_y, self.view.py_width, self.view.py_height + self.view.py_y); - self.navigationController.navigationBar.barTintColor = PYSEARCH_COLOR(249, 249, 249); + if (!self.navigationController.navigationBar.barTintColor) { // 用户没有设置 + self.navigationController.navigationBar.barTintColor = PYSEARCH_COLOR(249, 249, 249); + } } } @@ -184,7 +186,7 @@ - (UIButton *)emptyButton emptyButton.titleLabel.font = self.searchHistoryHeader.font; [emptyButton setTitleColor:PYTextColor forState:UIControlStateNormal]; [emptyButton setTitle:[NSBundle py_localizedStringForKey:PYSearchEmptyButtonText] forState:UIControlStateNormal]; - [emptyButton setImage:[NSBundle py_imageNamed: @"empty"] forState:UIControlStateNormal]; + [emptyButton setImage:[NSBundle py_imageNamed:@"empty"] forState:UIControlStateNormal]; [emptyButton addTarget:self action:@selector(emptySearchHistoryDidClick) forControlEvents:UIControlEventTouchUpInside]; [emptyButton sizeToFit]; emptyButton.py_width += PYSEARCH_MARGIN; @@ -314,7 +316,7 @@ - (void)setup [titleView addConstraint:xCons]; [titleView addConstraint:yCons]; searchBar.placeholder = [NSBundle py_localizedStringForKey:PYSearchSearchPlaceholderText]; - searchBar.backgroundImage = [NSBundle py_imageNamed: @"clearImage"]; + searchBar.backgroundImage = [NSBundle py_imageNamed:@"clearImage"]; searchBar.delegate = self; self.searchBar = searchBar; @@ -417,7 +419,7 @@ - (void)setupHotSearchRectangleTags self.baseSearchTableView.tableHeaderView.py_height = self.headerView.py_height = MAX(CGRectGetMaxY(self.hotSearchView.frame), CGRectGetMaxY(self.searchHistoryView.frame)); // 添加分割线 for (int i = 0; i < PYRectangleTagMaxCol - 1; i++) { // 添加垂直分割线 - UIImageView *verticalLine = [[UIImageView alloc] initWithImage:[NSBundle py_imageNamed: @"cell-content-line-vertical"]]; + UIImageView *verticalLine = [[UIImageView alloc] initWithImage:[NSBundle py_imageNamed:@"cell-content-line-vertical"]]; verticalLine.py_height = contentView.py_height; verticalLine.alpha = 0.7; verticalLine.py_x = contentView.py_width / PYRectangleTagMaxCol * (i + 1); @@ -425,7 +427,7 @@ - (void)setupHotSearchRectangleTags [contentView addSubview:verticalLine]; } for (int i = 0; i < ceil(((double)self.hotSearches.count / PYRectangleTagMaxCol)) - 1; i++) { // 添加水平分割线, ceil():向上取整函数 - UIImageView *verticalLine = [[UIImageView alloc] initWithImage:[NSBundle py_imageNamed: @"cell-content-line"]]; + UIImageView *verticalLine = [[UIImageView alloc] initWithImage:[NSBundle py_imageNamed:@"cell-content-line"]]; verticalLine.py_height = 0.5; verticalLine.alpha = 0.7; verticalLine.py_y = rectangleTagH * (i + 1); @@ -481,7 +483,7 @@ - (void)setupHotSearchRankTags [rankTextLabelsM addObject:rankTextLabel]; [rankView addSubview:rankTextLabel]; // 添加分割线 - UIImageView *line = [[UIImageView alloc] initWithImage:[NSBundle py_imageNamed: @"cell-content-line"]]; + UIImageView *line = [[UIImageView alloc] initWithImage:[NSBundle py_imageNamed:@"cell-content-line"]]; line.py_height = 0.5; line.alpha = 0.7; line.py_x = -PYScreenW * 0.5; @@ -1159,13 +1161,13 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N UIButton *closetButton = [[UIButton alloc] init]; // 设置图片容器大小、图片原图居中 closetButton.py_size = CGSizeMake(cell.py_height, cell.py_height); - [closetButton setImage:[NSBundle py_imageNamed: @"close"] forState:UIControlStateNormal]; - UIImageView *closeView = [[UIImageView alloc] initWithImage:[NSBundle py_imageNamed: @"close"]]; + [closetButton setImage:[NSBundle py_imageNamed:@"close"] forState:UIControlStateNormal]; + UIImageView *closeView = [[UIImageView alloc] initWithImage:[NSBundle py_imageNamed:@"close"]]; [closetButton addTarget:self action:@selector(closeDidClick:) forControlEvents:UIControlEventTouchUpInside]; closeView.contentMode = UIViewContentModeCenter; cell.accessoryView = closetButton; // 添加分割线 - UIImageView *line = [[UIImageView alloc] initWithImage:[NSBundle py_imageNamed: @"cell-content-line"]]; + UIImageView *line = [[UIImageView alloc] initWithImage:[NSBundle py_imageNamed:@"cell-content-line"]]; line.py_height = 0.5; line.alpha = 0.7; line.py_x = PYSEARCH_MARGIN; @@ -1175,7 +1177,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N } // 设置数据 - cell.imageView.image = [NSBundle py_imageNamed: @"search_history"]; + cell.imageView.image = [NSBundle py_imageNamed:@"search_history"]; cell.textLabel.text = self.searchHistories[indexPath.row]; return cell; diff --git a/PYSearch/UIColor+PYSearchExtension.h b/PYSearch/UIColor+PYSearchExtension.h index 5388643..39130f7 100644 --- a/PYSearch/UIColor+PYSearchExtension.h +++ b/PYSearch/UIColor+PYSearchExtension.h @@ -8,6 +8,7 @@ #import @interface UIColor (PYSearchExtension) + /** 根据16进制字符串返回对应颜色 */ + (instancetype)py_colorWithHexString:(NSString *)hexString; diff --git a/PYSearch/UIView+PYSearchExtension.h b/PYSearch/UIView+PYSearchExtension.h index d0b684a..a125c4a 100755 --- a/PYSearch/UIView+PYSearchExtension.h +++ b/PYSearch/UIView+PYSearchExtension.h @@ -8,6 +8,7 @@ #import @interface UIView (PYSearchExtension) + @property (nonatomic, assign) CGFloat py_x; @property (nonatomic, assign) CGFloat py_y; @property (nonatomic, assign) CGFloat py_centerX; @@ -17,10 +18,4 @@ @property (nonatomic, assign) CGSize py_size; @property (nonatomic, assign) CGPoint py_origin; -/** 设置锚点 */ -- (CGPoint)py_setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view; - -/** 根据手势触摸点修改相应的锚点,就是沿着触摸点对self做相应的手势操作 */ -- (CGPoint)py_setAnchorPointBaseOnGestureRecognizer:(UIGestureRecognizer *)gr; - @end diff --git a/PYSearch/UIView+PYSearchExtension.m b/PYSearch/UIView+PYSearchExtension.m index 8c3d8d7..b7e4225 100755 --- a/PYSearch/UIView+PYSearchExtension.m +++ b/PYSearch/UIView+PYSearchExtension.m @@ -89,7 +89,8 @@ - (void)setPy_width:(CGFloat)py_width self.frame = frame; } -- (CGFloat)py_width + +-(CGFloat)py_width { return self.frame.size.width; } @@ -106,54 +107,4 @@ - (CGPoint)py_origin return self.frame.origin; } -/** 设置锚点 */ -- (CGPoint)py_setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view -{ - CGPoint oldOrigin = view.frame.origin; - view.layer.anchorPoint = anchorPoint; - CGPoint newOrigin = view.frame.origin; - - CGPoint transition; - transition.x = newOrigin.x - oldOrigin.x; - transition.y = newOrigin.y - oldOrigin.y; - - view.center = CGPointMake (view.center.x - transition.x, view.center.y - transition.y); - return self.layer.anchorPoint; -} - -/** 根据手势触摸点修改相应的锚点,就是沿着触摸点做相应的手势操作 */ -- (CGPoint)py_setAnchorPointBaseOnGestureRecognizer:(UIGestureRecognizer *)gr -{ - // 手势为空 直接返回 - if (!gr) return CGPointMake(0.5, 0.5); - - // 创建锚点 - CGPoint anchorPoint; - if ([gr isKindOfClass:[UIPinchGestureRecognizer class]]) { // 捏合手势 - if (gr.numberOfTouches == 2) { - // 当触摸开始时,获取两个触摸点 - CGPoint point1 = [gr locationOfTouch:0 inView:gr.view]; - CGPoint point2 = [gr locationOfTouch:1 inView:gr.view]; - anchorPoint.x = (point1.x + point2.x) / 2 / gr.view.py_width; - anchorPoint.y = (point1.y + point2.y) / 2 / gr.view.py_height; - } - } else if ([gr isKindOfClass:[UITapGestureRecognizer class]]) { // 点击手势 - // 获取触摸点 - CGPoint point = [gr locationOfTouch:0 inView:gr.view]; - - CGFloat angle = acosf(gr.view.transform.a); - if (ABS(asinf(gr.view.transform.b) + M_PI_2) < 0.01) angle += M_PI; - CGFloat width = gr.view.py_width; - CGFloat height = gr.view.py_height; - if (ABS(angle - M_PI_2) <= 0.01 || ABS(angle - M_PI_2 * 3) <= 0.01) { // 旋转角为90° - // width 和 height 对换 - width = gr.view.py_height; - height = gr.view.py_width; - } - // 如果旋转了 - anchorPoint.x = point.x / width; - anchorPoint.y = point.y / height; - }; - return [self py_setAnchorPoint:anchorPoint forView:self]; -} @end diff --git a/PYSearchExample/PYSearchExample/PYSearchExampleController.m b/PYSearchExample/PYSearchExample/PYSearchExampleController.m index 5c50067..c1d383c 100644 --- a/PYSearchExample/PYSearchExample/PYSearchExampleController.m +++ b/PYSearchExample/PYSearchExample/PYSearchExampleController.m @@ -51,7 +51,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { - // 1.创建热门搜索 + // 1. 创建热门搜索 NSArray *hotSeaches = @[@"Java", @"Python", @"Objective-C", @"Swift", @"C", @"C++", @"PHP", @"C#", @"Perl", @"Go", @"JavaScript", @"R", @"Ruby", @"MATLAB"]; // 2. 创建控制器 PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:hotSeaches searchBarPlaceholder:NSLocalizedString(@"PYExampleSearchPlaceholderText", @"搜索编程语言") didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {