Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions ChooseLocation/ChooseLocation/View/ChooseLocationView.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@

@property (nonatomic,copy) NSString * areaCode;

- (instancetype)initWithFrame:(CGRect)frame withScaling:(CGFloat)scaling;

@end
27 changes: 13 additions & 14 deletions ChooseLocation/ChooseLocation/View/ChooseLocationView.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ @interface ChooseLocationView ()<UITableViewDataSource,UITableViewDelegate,UIScr
@property (nonatomic,strong) NSMutableArray * tableViews;
@property (nonatomic,strong) NSMutableArray * topTabbarItems;
@property (nonatomic,weak) UIButton * selectedBtn;
@property (nonatomic, assign) CGFloat scaledScreenWidth;
@end

@implementation ChooseLocationView

- (instancetype)initWithFrame:(CGRect)frame
{
- (instancetype)initWithFrame:(CGRect)frame withScaling:(CGFloat)scaling {
self = [super initWithFrame:frame];
if (self) {
_scaledScreenWidth = HYScreenW * (2.0 - scaling);
[self setUp];
}
return self;
Expand All @@ -44,7 +45,6 @@ - (instancetype)initWithFrame:(CGRect)frame
#pragma mark - setUp UI

- (void)setUp{

UIView * topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, kHYTopViewHeight)];
[self addSubview:topView];
UILabel * titleLabel = [[UILabel alloc]init];
Expand All @@ -58,7 +58,6 @@ - (void)setUp{
separateLine.top = topView.height - separateLine.height;
topView.backgroundColor = [UIColor whiteColor];


AddressView * topTabbar = [[AddressView alloc]initWithFrame:CGRectMake(0, topView.height, self.frame.size.width, kHYTopViewHeight)];
[self addSubview:topTabbar];
_topTabbar = topTabbar;
Expand All @@ -79,7 +78,7 @@ - (void)setUp{

_underLine.backgroundColor = [UIColor orangeColor];
UIScrollView * contentView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(topTabbar.frame), self.frame.size.width, self.height - kHYTopViewHeight - kHYTopTabbarHeight)];
contentView.contentSize = CGSizeMake(HYScreenW, 0);
contentView.contentSize = CGSizeMake(_scaledScreenWidth, 0);
[self addSubview:contentView];
_contentView = contentView;
_contentView.pagingEnabled = YES;
Expand All @@ -91,7 +90,7 @@ - (void)setUp{

- (void)addTableView{

UITableView * tabbleView = [[UITableView alloc]initWithFrame:CGRectMake(self.tableViews.count * HYScreenW, 0, HYScreenW, _contentView.height)];
UITableView * tabbleView = [[UITableView alloc]initWithFrame:CGRectMake(self.tableViews.count * _scaledScreenWidth, 0, _scaledScreenWidth, _contentView.height)];
[_contentView addSubview:tabbleView];
[self.tableViews addObject:tabbleView];
tabbleView.separatorStyle = UITableViewCellSeparatorStyleNone;
Expand Down Expand Up @@ -269,7 +268,7 @@ - (void)topBarItemClick:(UIButton *)btn{
NSInteger index = [self.topTabbarItems indexOfObject:btn];

[UIView animateWithDuration:0.5 animations:^{
self.contentView.contentOffset = CGPointMake(index * HYScreenW, 0);
self.contentView.contentOffset = CGPointMake(index * _scaledScreenWidth, 0);
[self changeUnderLineFrame:btn];
}];
}
Expand All @@ -287,7 +286,7 @@ - (void)changeUnderLineFrame:(UIButton *)btn{
//完成地址选择,执行chooseFinish代码块
- (void)setUpAddress:(NSString *)address{

NSInteger index = self.contentView.contentOffset.x / HYScreenW;
NSInteger index = self.contentView.contentOffset.x / _scaledScreenWidth;
UIButton * btn = self.topTabbarItems[index];
[btn setTitle:address forState:UIControlStateNormal];
[btn sizeToFit];
Expand Down Expand Up @@ -324,15 +323,15 @@ - (void)removeLastItem{
//滚动到下级界面,并重新设置顶部按钮条上对应按钮的title
- (void)scrollToNextItem:(NSString *)preTitle{

NSInteger index = self.contentView.contentOffset.x / HYScreenW;
NSInteger index = self.contentView.contentOffset.x / _scaledScreenWidth;
UIButton * btn = self.topTabbarItems[index];
[btn setTitle:preTitle forState:UIControlStateNormal];
[btn sizeToFit];
[_topTabbar layoutIfNeeded];
[UIView animateWithDuration:0.25 animations:^{
self.contentView.contentSize = (CGSize){self.tableViews.count * HYScreenW,0};
self.contentView.contentSize = (CGSize){self.tableViews.count * _scaledScreenWidth,0};
CGPoint offset = self.contentView.contentOffset;
self.contentView.contentOffset = CGPointMake(offset.x + HYScreenW, offset.y);
self.contentView.contentOffset = CGPointMake(offset.x + _scaledScreenWidth, offset.y);
[self changeUnderLineFrame: [self.topTabbar.subviews lastObject]];
}];
}
Expand All @@ -344,7 +343,7 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
if(scrollView != self.contentView) return;
__weak typeof(self)weakSelf = self;
[UIView animateWithDuration:0.25 animations:^{
NSInteger index = scrollView.contentOffset.x / HYScreenW;
NSInteger index = scrollView.contentOffset.x / _scaledScreenWidth;
UIButton * btn = weakSelf.topTabbarItems[index];
[weakSelf changeUnderLineFrame:btn];
}];
Expand Down Expand Up @@ -385,9 +384,9 @@ - (void)setAreaCode:(NSString *)areaCode{
[self changeUnderLineFrame:lastBtn];

//2.4 设置偏移量
self.contentView.contentSize = (CGSize){self.tableViews.count * HYScreenW,0};
self.contentView.contentSize = (CGSize){self.tableViews.count * _scaledScreenWidth,0};
CGPoint offset = self.contentView.contentOffset;
self.contentView.contentOffset = CGPointMake((self.tableViews.count - 1) * HYScreenW, offset.y);
self.contentView.contentOffset = CGPointMake((self.tableViews.count - 1) * _scaledScreenWidth, offset.y);

[self setSelectedProvince:provinceName andCity:cityName andDistrict:districtName];
}
Expand Down
15 changes: 12 additions & 3 deletions ChooseLocation/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
#import "ViewController.h"
#import "ChooseLocationView.h"
#import "CitiesDataTool.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
const static CGFloat SCALING = 0.95;

@interface ViewController ()<NSURLSessionDelegate,UIGestureRecognizerDelegate>
@property (nonatomic,strong) ChooseLocationView *chooseLocationView;
@property (nonatomic,strong) UIView *cover;
Expand All @@ -29,7 +34,8 @@ - (void)viewDidLoad {
- (IBAction)chooseLocation:(UIButton *)sender {

[UIView animateWithDuration:0.25 animations:^{
self.view.transform =CGAffineTransformMakeScale(0.95, 0.95);
self.view.transform =CGAffineTransformMakeScale(SCALING, SCALING);
self.chooseLocationView.frame = CGRectMake(-(kScreenWidth * (1 - SCALING)), kScreenHeight - 350, kScreenWidth * (2.0 - SCALING), 350);
}];
self.cover.hidden = !self.cover.hidden;
self.chooseLocationView.hidden = self.cover.hidden;
Expand All @@ -56,8 +62,7 @@ - (void)tapCover:(UITapGestureRecognizer *)tap{
- (ChooseLocationView *)chooseLocationView{

if (!_chooseLocationView) {
_chooseLocationView = [[ChooseLocationView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 350, [UIScreen mainScreen].bounds.size.width, 350)];

_chooseLocationView = [[ChooseLocationView alloc]initWithFrame:CGRectMake(-(kScreenWidth * (1 - SCALING)), kScreenHeight, kScreenWidth * (2.0 - SCALING), 350) withScaling:SCALING];
}
return _chooseLocationView;
}
Expand All @@ -73,6 +78,9 @@ - (UIView *)cover{
[UIView animateWithDuration:0.25 animations:^{
weakSelf.addresslabel.text = weakSelf.chooseLocationView.address;
weakSelf.view.transform = CGAffineTransformIdentity;
weakSelf.cover.alpha = 0;
weakSelf.chooseLocationView.frame = CGRectMake(-(kScreenWidth * (1 - SCALING)), kScreenHeight, kScreenWidth * (2.0 - SCALING), 350);
} completion:^(BOOL finished) {
weakSelf.cover.hidden = YES;
}];
};
Expand All @@ -81,6 +89,7 @@ - (UIView *)cover{
tap.delegate = self;
_cover.hidden = YES;
}
_cover.alpha = 1;
return _cover;
}
@end