iOS小项目IOSiOS-Vendor

高仿美团<三>

2015-09-12  本文已影响5845人  土鳖不土

亲爱的粉友们,高仿美团<三>终于和大家见面了

首先感谢你们的一直关注。同时也感谢我自己。
曾经c++挂科,java接近挂科,英语挂科等等。大学里四年每年都挂科。看到代码就头疼就回避,但是我很自信的。从来都没觉得自己不行。我坚信我想要做的事,只要就动手去做,并坚持下去一定会有成果的。哪怕是一点点也会有无限的成就感。或许现在的我们距大牛还很远。so what?
这篇博文最想和你们分享的不是怎么实现这些功能 而是一句话:

相信自己, 别人可以做到,你也可以。


这里也要谢谢iOS让我觉得原来代码可以这么玩,这么有趣。
接下里跟你们说说美三的主要功能的实现,一个一个来:如下选选择地址


选择地址 屏幕快照 2015-09-11 21.46.59.png

如上界面截图一眼望上去实现方案如下:
1.创建N个button,或者xib拉N个button
2.UICollectionView
3.for循环九宫格创建button
第一种SB才这么干,第二种吃饱了撑着(杀鸡焉用宰牛刀?)
所以我用第三种代码如下:


//三列
     int totalloc = 3;
    
     CGFloat buttonw = 70;
     CGFloat buttonh = 30;
    
    //算出间距 self.frame.size.width
     CGFloat margin=(self.frame.size.width - totalloc * buttonw) / (totalloc + 1);
     //int count=11;
     for (int i = 0; i < areaArray.count; i++) {
         NSDictionary *dict = areaArray[i];
         NSString *titleStr = dict[@"areaName"];
         int row = i / totalloc;//行号
         //0/3=0,1/3=0,2/3=1; (i 从0到2都是第一行) i= 3的时候第二行
         
         int loc = i % totalloc;//列号
         
         //横坐标
         CGFloat buttonx = margin + (margin + buttonw) * loc;
         
         //纵坐标
         CGFloat buttony = margin + (margin + buttonh) * row;
         
         
         //创建uiview控件
         UIButton *areaButton=[[UIButton alloc]initWithFrame:CGRectMake(buttonx, buttony, buttonw, buttonh)];
         [areaButton setTitle:titleStr forState:UIControlStateNormal];
         [areaButton setBackgroundColor: navigationBarColor];
         [areaButton addTarget:self action:@selector(areaButtonClick:) forControlEvents:UIControlEventTouchUpInside];
         [self.scrollView addSubview:areaButton];
屏幕快照 2015-09-11 22.02.41.png

这个界面的热门实现也是用的九宫格创建右边的“热ABCD。。。”
用的如下方法:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return _keys;
}

点击之后回到主界面的传值,用的是block传值
定义一个block 声明该block对象

typedef void(^JFSendCityNameBlock)(NSString *cityNameStr);

@property (nonatomic , copy)JFSendCityNameBlock sendCityBlock;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section >0) {
        NSString *key = _keys[indexPath.section] ;
        NSString *cityName = [_cities objectForKey:key][indexPath.row];
        JFLog(@"%@", cityName);
##这个地方调用block用来传值
        self.sendCityBlock (cityName);
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    }
}
#pragma mark -changeCityButtonClickDelegate
-(void)changCityButtonClick:(UIButton *)sender{
    JFShowAllCityViewController *showAllCityVC = [[JFShowAllCityViewController alloc]init];
 ###再要跳转得倒这个控制器对象的时候给他用block回调传值   
    showAllCityVC.sendCityBlock = ^(NSString *str){
        [self.leftButton setTitle:str forState:UIControlStateNormal];
        JFLog(@"主界面%@,",str);
    };
    [self presentViewController:showAllCityVC animated:YES completion:^{
        
    }];
//    [self.navigationController pushViewController:showAllCityVC animated:YES];
    
}

webview

接下里说说下面这个界面这个界面用webView做的没啥技术含量
说下隐藏底部导航按钮

/**
 *  隐藏底部导航条
 */
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

webView代理方法给给当前的控制器title赋值

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    JFLog(@"加载webview完成");
    NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    _titleLabel.text = theTitle;
    [_activityView stopAnimating];
    
}

自定义大头针气泡

我要重点说下上面这个界面,是我困惑了很久的一个鬼东西百度地图自定义气泡,记得之前问一个简友,让我很郁闷。问他一个问题什么都解释不清楚。

我会独立一篇出来讲解这个东东

主要思路如下:
1.创建一个数据模型(里面友经纬度,气泡上的数据)
项目中用:JFDataModel类
2.继承BMKPointAnnotation(里面声明一个数据模型属性用来传递模型数据)
项目中用:JFAnnotation类
3.画气泡图:(所谓自定义气泡,就是根据需求来自 定义)
项目中用:JFCalloutView类
代码如下:

//画气泡
-(void)drawRect:(CGRect)rect{
    [self drawInContext:UIGraphicsGetCurrentContext()];
    
    self.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}

-(void)drawInContext:(CGContextRef)context{
    CGContextSetLineWidth(context, 2.0);
    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.8].CGColor);
    //
    [self getDrawPath:context];
    CGContextFillPath(context);
}

-(void)getDrawPath:(CGContextRef)context{
    
    CGRect rrect = self.bounds;
    CGFloat radius = 6.0;
    CGFloat minx = CGRectGetMinX(rrect),
    midx = CGRectGetMidX(rrect),
    maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect),
    maxy = CGRectGetMaxY(rrect)-kArrorHeight;
    
    //画向下的三角形
    CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
    CGContextAddLineToPoint(context, midx, maxy+kArrorHeight);//画直线
    CGContextAddLineToPoint(context, midx-kArrorHeight, maxy);
    
    //画4个圆弧
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);//画完后 current point不在minx,miny,而是在圆弧结束的地方
    CGContextAddArcToPoint(context, minx, miny, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxy, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextClosePath(context);
}

4.创建标注(就是创建大头针)
项目中用 JFAnnotationView
关键代码如下

/**
 *  通过mapView快速创建一个annotationView
 */
+(instancetype)annotationViewWithMapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]])
    {
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        JFAnnotationView *annotationView = (JFAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[JFAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];
        }
        if ([annotation isKindOfClass:[JFAnnotation class]]){
            annotationView.jfannotation = (JFAnnotation *)annotation;
        }
        annotationView.canShowCallout = NO;
        annotationView.image = [UIImage imageNamed:@"icon_map_cateid_1"];

        // 设置中⼼心点偏移,使得标注底部中间点成为经纬度对应点
        
        // annotationView.centerOffset = CGPointMake(0, -18);
        return annotationView;
    }
    return nil;   
}
上门服务

这个界面的点击事件也是 用的webview做的没啥好讲的

切换

这个界面只是预留了界面供下一个版本的控制器


接下里说说这个界面的功能


搜索

代码如下:

#pragma mark -UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    if (textField.text.length) {
        for (NSString *text in self.hisDatas) {
            if ([text isEqualToString:textField.text]) {
                return YES;
            }
        }
        //输入之后立马插到数组 然后写入文件
        [self.hisDatas insertObject:textField.text atIndex:0];
        [self.hisDatas writeToFile:JFSearchHistoryPath atomically:YES];
        [self.searchTableView reloadData];
        textField.text = @"";
    }
    
    return YES;
}

没搜索一个我就把他插到文件里面,也是数据持久化的一个应用了
然后每次打开这个界面的时候就从新读取这个文件转为数组,再加载到tableView上这个功能有点像一个简友:尼维的小熊的高仿城觅,搜索功能,是的这个功能是看他的,但是我做了另一些封装。

详情

详细源码我已经放到github上面了里面已经解释的很清楚了

本项目会继续更新下去的,希望你继续关注我哦,我一定会分享更好的文章更好的项目供大家一起学习。

如果你觉得还不错就给我一个star或者给我提意见我都会非常感谢。

上一篇 下一篇

猜你喜欢

热点阅读