Skip to content

Commit

Permalink
Optimization code
Browse files Browse the repository at this point in the history
Optimization code
  • Loading branch information
ko1o committed Feb 15, 2017
1 parent 3da2332 commit 467a074
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 77 deletions.
5 changes: 3 additions & 2 deletions PYSearch/NSBundle+PYSearchExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

/** 获取PYSearch.bundle路径 */
+ (NSBundle *)py_searchBundle;

+ (UIImage*)py_imageNamed:(NSString*)name;

/** 获取PYSearch.bundle路径中的图片 */
+ (UIImage *)py_imageNamed:(NSString *)name;

@end
6 changes: 2 additions & 4 deletions PYSearch/NSBundle+PYSearchExtension.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions PYSearch/PYSearchSuggestionViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef void(^PYSearchSuggestionDidSelectCellBlock)(UITableViewCell *selectedCel
@end

@interface PYSearchSuggestionViewController : UITableViewController

/** 搜索建议数据源 */
@property (nonatomic, weak) id<PYSearchSuggestionViewDataSource> dataSource;
/** 搜索建议 */
Expand Down
6 changes: 3 additions & 3 deletions PYSearch/PYSearchSuggestionViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "PYSearchConst.h"

@interface PYSearchSuggestionViewController ()

/** 记录消失前的contentInset */
@property (nonatomic, assign) UIEdgeInsets originalContentInset;

Expand Down Expand Up @@ -74,7 +75,6 @@ - (void)setSearchSuggestions:(NSArray<NSString *> *)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:)]) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
22 changes: 12 additions & 10 deletions PYSearch/PYSearchViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -417,15 +419,15 @@ - (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);
verticalLine.py_width = 0.5;
[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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions PYSearch/UIColor+PYSearchExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import <UIKit/UIKit.h>

@interface UIColor (PYSearchExtension)

/** 根据16进制字符串返回对应颜色 */
+ (instancetype)py_colorWithHexString:(NSString *)hexString;

Expand Down
7 changes: 1 addition & 6 deletions PYSearch/UIView+PYSearchExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import <UIKit/UIKit.h>

@interface UIView (PYSearchExtension)

@property (nonatomic, assign) CGFloat py_x;
@property (nonatomic, assign) CGFloat py_y;
@property (nonatomic, assign) CGFloat py_centerX;
Expand All @@ -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
53 changes: 2 additions & 51 deletions PYSearch/UIView+PYSearchExtension.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ - (void)setPy_width:(CGFloat)py_width
self.frame = frame;

}
- (CGFloat)py_width

-(CGFloat)py_width
{
return self.frame.size.width;
}
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 467a074

Please sign in to comment.