您的位置:首页 > 其它

模仿共享单车

2017-11-05 23:06 162 查看


//AppDelegate.h
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "RESideMenu.h" //抽屉视图第三方

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property(strong,nonatomic)RESideMenu *sideMenu;

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong) NSPersistentContainer *persistentContainer;

- (void)saveContext;

@end

//AppDelegate.m

#import "AppDelegate.h"
#import "LeftMenuViewController.h"
#import "QQQViewController.h"
#import "MapViewController.h"
#import "GuideViewController.h"
@interface AppDelegate ()
@property(nonatomic,strong)LeftMenuViewController *leftVC ; //左侧菜单控制器
@property(nonatomic,strong)UITabBarController *tabBarCtl;//标签栏控制器

@end

@implementation AppDelegate
//私有方法,用于生成一个导航控制器 父类指向子类, 叫做多态
-(UINavigationController *)creatNavigationWithController:(UIViewController *)vc title:(NSString *)title image:(NSString *)imgName selectImage:(NSString *)selImgName{

UINavigationController *nav  =[[UINavigationController alloc]initWithRootViewController:vc] ;
vc.navigationItem.title = title ;
nav.tabBarItem = [[UITabBarItem alloc]initWithTitle:title image:[UIImage imageNamed:imgName] selectedImage:[UIImage imageNamed:selImgName]];
return nav;
}
//左侧菜单
-(LeftMenuViewController *)leftVC {
if (!_leftVC) {
_leftVC = [[LeftMenuViewController alloc]init];
}
return _leftVC ;
}
//标签栏控制器
-(UITabBarController *)tabBarCtl {
if (!_tabBarCtl) {
_tabBarCtl = [[UITabBarController alloc]init] ;
UINavigationController *newsNav = [self creatNavigationWithController:[[MapViewController alloc]init] title:@"地图" image:@"TabBar_home_1@2x" selectImage:@"TabBar_home_2@2x"] ;

_tabBarCtl.viewControllers = @[newsNav];
}
return _tabBarCtl ;
}
//get重写 侧滑
-(RESideMenu *)sideMenu {
if (!_sideMenu) {
_sideMenu = [[RESideMenu alloc]initWithContentViewController:self.tabBarCtl leftMenuViewController:self.leftVC rightMenuViewController:nil] ;
_sideMenu.backgroundImage = [UIImage imageNamed:@"Stars@2x"];
//设置不能缩放
_sideMenu.scaleContentView = NO ;
_sideMenu.contentViewInPortraitOffsetCenterX = FIT_X(100); //竖屏
}
return _sideMenu ;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
GuideViewController *guideVC = [[GuideViewController alloc]init];

//第一次运行app
if ( ![[NSUserDefaults standardUserDefaults]objectForKey:NOT_FIRST_LANUCH]) {
//进入引导页控制器
self.window.rootViewController = guideVC ;
}
//非首次运行app
else {

//获取持久化的版本号数据
NSString *savedVersion = [[NSUserDefaults standardUserDefaults]objectForKey:NOT_FIRST_LANUCH];
//判断版本号是否一致
if ([savedVersion isEqualToString:VERSION_CURRENT]) {
//没有更新,把主页作为窗口的跟视图控制器
self.window.rootViewController = self.sideMenu;
}
else{
//更新后,把引导页作为窗口的跟视图控制器
self.window.rootViewController = guideVC ;
}
}

return YES;
}

//ViewController.h和ViewController.m都为空

//空

//创建一个pch文件

//Stary.pch内容

#ifndef Stary_pch
#define Stary_pch
#import "BaseViewController.h"

/*
-----------用于适配的宏
*/
//屏幕宽度的宏
#define SCREEN_W  [UIScreen mainScreen].bounds.size.wid
4000
th
//屏幕宽度的高
#define SCREEN_H  [UIScreen mainScreen].bounds.size.height

//适配x轴的宏
#define FIT_X(w)  (SCREEN_W / 375. * (w))
//适配y轴的宏
#define FIT_Y(h)  (SCREEN_H / 667. * (h))

/*
----------当前版本号的宏
*/

#define VERSION_CURRENT [[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleShortVersionString"]

#define NOT_FIRST_LANUCH @"NotFirstLanuch"

#define App_Delegate (AppDelegate *)[UIApplication sharedApplication].delegate
#endif

//GuideViewController.h
//GuideViewController.m
#import "GuideViewController.h"
#import "ImageScrollView.h"
#import "AppDelegate.h"
@interface GuideViewController ()<ImageScrollViewDelegate>

@end

@implementation GuideViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//调用image第三方
NSArray *imgArr = @[@"guidePage1.png",@"guidePage2.png",@"guidePage3.png"] ;
ImageScrollView *imgScrView = [[ImageScrollView alloc]initWithFrame:self.view.frame style:ImageScrollType_Guide images:imgArr confirmBtnTitle:@"立即体验" confirmBtnTitleColor:[UIColor whiteColor] confirmBtnFrame:CGRectMake(FIT_X(260), FIT_Y(30), FIT_X(120), FIT_Y(40)) autoScrollTimeInterval:0.0 delegate: self ];
[self.view addSubview:imgScrView] ;
[imgScrView addPageControlToSuperView:self.view] ;
}
#pragma -mark 实现协议imageScrollViewDelegate
//立即体验按钮触发
-(void)experienceDidHandle {
//获取当前版本号
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults] ;
[ud setObject:VERSION_CURRENT forKey:NOT_FIRST_LANUCH] ;
[ud synchronize] ;
/*
切换窗口的很视图控制器
*/
//获取整个应用程序的代理对象
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate ;
appDelegate.window.rootViewController = appDelegate.sideMenu;
}
//BaseViewController.h
//BaseViewController.m
#import "BaseViewController.h"
#import "AppDelegate.h"
@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
/*
二级返回按钮的设置
*/
if (self.navigationController.viewControllers.count == 1) {
UIButton *headBtn = [UIButton buttonWithType:UIButtonTypeCustom];
headBtn.frame = CGRectMake(0, 0, 40, 40) ;
headBtn.clipsToBounds = YES ;
headBtn.layer.cornerRadius = 40 / 2 ;
[headBtn setImage:[UIImage imageNamed:@"40"] forState:UIControlStateNormal];
[headBtn addTarget:self action:@selector(headBtnHandle:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem =  [[UIBarButtonItem alloc]initWithCustomView:headBtn];
}
else {
self.navigationItem.hidesBackButton  = YES ;
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(0, 0, 15, 10);
[backBtn setImage:[UIImage imageNamed:@"navigationItem_back"] forState:UIControlStateNormal] ;
[backBtn addTarget:self action:@selector(backBtnHandle:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:backBtn] ;

}

}
#pragma mark -------触发方法 ----------
//头像触发时间
-(void)headBtnHandle:(id)sender{
AppDelegate *appDele = App_Delegate ;
[appDele.sideMenu presentLeftMenuViewController];
}
//返回方法
-(void)backBtnHandle:(id)sender{
[self.navigationController popViewControllerAnimated:YES] ;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated] ;

/*
进入二级视图不显示导航控制器
*/
// 判断导航栈中有几个控制器
if (self.navigationController.viewControllers.count == 1) {
//表示该视图是导航的跟视图控制器
self.tabBarController.tabBar.hidden = NO ;
}
else {
//表示该视图是导航的2级或者3级视图控制器
self.navigationController.tabBarController.tabBar.hidden = YES ;
}

}

//LeftMenuViewController.h
//LeftMenuViewController.m
#import "LeftMenuViewController.h"

@interface LeftMenuViewController ()<UITableViewDelegate,UITableViewDataSource>{
NSArray *_tableDatas ; //表格数据
}

//头像视图
@property(nonatomic,strong)UIImageView *headImgView ;
//表格
@property(nonatomic,strong)UITableView *table ;
@end

@implementation LeftMenuViewController
#pragma mark ----------控件实例化-----------------
-(UIImageView *)headImgView {
if (!_headImgView) {
_headImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, FIT_X(80), FIT_Y(80))] ;
_headImgView.center = CGPointMake(SCREEN_W/4+FIT_X(50), FIT_Y(104)) ;
_headImgView.clipsToBounds = YES ;
_headImgView.layer.cornerRadius = FIT_X(40) ;
_headImgView.image = [UIImage imageNamed:@"40"] ;

_headImgView.userInteractionEnabled = YES  ;
//加手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headImgDidHandle:)];
[_headImgView addGestureRecognizer:tap] ;
}
return _headImgView ;
}

-(UITableView *)table {
if (!_table) {
_table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, FIT_X(300), 200) style:UITableViewStylePlain] ;
_table.rowHeight = 50;
_table.center = CGPointMake(self.headImgView.center.x, self.headImgView.center.y+self.headImgView.frame.size.height/2+100+FIT_Y(20)) ;
//不可滑动
_table.scrollEnabled  = NO ;
_table.backgroundColor = [UIColor clearColor];
_table.dataSource  = self ;
_table.delegate = self;

}
return _table ;
}

#pragma mark ----------UITableViewDelegate,UITableViewDataSource-----------------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _tableDatas.count ;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
}
cell.textLabel.text = _tableDatas[indexPath.row] ;
cell.backgroundColor = [UIColor clearColor] ;
cell.textLabel.textColor = [UIColor whiteColor] ;
cell.textLabel.font =[UIFont systemFontOfSize:18];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
//点击不变色.
cell.selectionStyle = UITableViewCellSelectionStyleNone;

return cell ;
}
#pragma mark ----------触发事件-----------------
//头像点击方法
-(void)headImgDidHandle:(id)sender{
NSLog(@"点击了按钮");

}
#pragma mark -------loadView ------
-(void)loadView {
[super loadView] ;
[self.view addSubview:self.headImgView];
[self.view addSubview:self.table] ;
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_tableDatas = @[@"我的钱包",@"我的卡卷",@"我的行程",@"邀请行程",@"你的",@"啦啦啦啦",@"我的旅途"] ;
[self.table reloadData] ;

}

//QQQViewController.h继承于BaseViewController
//QQQViewController.m
#import "QQQViewController.h"

@interface QQQViewController ()

@end

@implementation QQQViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor] ;

}

//MapViewController.h
MapViewController.m
#import "MapViewController.h"
#import "QQQViewController.h"
#import <MapKit/MapKit.h>//地图
#import <CoreLocation/CoreLocation.h>//定位

@interface MapViewController ()<MKMapViewDelegate>
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
//地理编码
@property (strong,nonatomic)CLGeocoder *geocoder;

- (IBAction)button:(id)sender;

@end

@implementation MapViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHandle:)] ;
[self.view addGestureRecognizer:tap];

//初始化地理编码
_geocoder = [[CLGeocoder alloc]init];
//设置地图的显示风格,此处设置使用标准地图
self.mapView.mapType = MKMapTypeStandard;
// 设置地图可缩放
self.mapView.zoomEnabled =YES;
// 设置地图可滚动
self.mapView.scrollEnabled =YES;
// 设置地图可旋转
self.mapView.rotateEnabled =YES;
//设置显示用户当前位置
self.mapView.showsUserLocation = YES;
//调用自己实现的方法设置地图的显示位置和显示区域
[self locateToLatitude:37.23 longitude:122.1234];
//创建一个手势处理器,用于检测、处理长按手势
UILongPressGestureRecognizer* gesture = [[UILongPressGestureRecognizer
alloc]initWithTarget:self action:@selector(longPress:)];
//为该控件添加手势处理器
[self.view addGestureRecognizer:gesture];

//遵守代理是要实现自定义锚点
self.mapView.delegate =self;
}

#pragma mark --手势回调
- (void) longPress:(UILongPressGestureRecognizer*)gesture{
//获取长按点的坐标
CGPoint pos = [gesture locationInView:self.mapView];
//将长按点的坐标转换为经度、维度值
CLLocationCoordinate2D coord = [self.mapView convertPoint:pos
toCoordinateFromView:self.mapView];
// 将经度、维度值包装为CLLocation对象
CLLocation* location = [[CLLocation alloc]initWithLatitude:coord.latitude
longitude:coord.longitude];
//根据经、纬度反向解析地址
[_geocoder reverseGeocodeLocation:location completionHandler:
^(NSArray *placemarks,NSError *error)
{
if (placemarks.count >0 && error ==nil)
{
// 获取解析得到的第一个地址信息
CLPlacemark* placemark = [placemarks objectAtIndex:0];
// 获取地址信息中的FormattedAddressLines对应的详细地址
NSArray* addrArray = placemark
.addressDictionary[@"FormattedAddressLines"];
// 将详细地址拼接成一个字符串
NSMutableString* address = [[NSMutableString alloc]init];
for(int i =0 ; i < addrArray.count ; i ++)
{
[address appendString:addrArray[i]];
}

// 创建MKPointAnnotation对象——代表一个锚点
MKPointAnnotation  *annotation = [[MKPointAnnotation alloc]init];

annotation.title = placemark.name;
annotation.subtitle = address;
annotation.coordinate = coord;
// 添加锚点
[self.mapView addAnnotation:annotation];
}
}];
}
#pragma mark --自定义封装定位方法
- (void)locateToLatitude:(CGFloat)latitude longitude:(CGFloat)longitude{
//设置地图中心的经、纬度
CLLocationCoordinate2D center = {latitude , longitude};
//设置地图显示的范围,
MKCoordinateSpan span;
//地图显示范围越小,细节越清楚
span.latitudeDelta =0.01;
span.longitudeDelta =0.01;
// 创建MKCoordinateRegion对象,该对象代表了地图的显示中心和显示范围。
MKCoordinateRegion region = {center,span};
//设置当前地图的显示中心和显示范围
[self.mapView setRegion:region animated:YES];

// 创建MKPointAnnotation对象——代表一个锚点
MKPointAnnotation* annotation = [[MKPointAnnotation alloc]init];
annotation.title =@"北京石羿科技发展有限公司";
annotation.subtitle =@"海淀区中关村软件园";

CLLocationCoordinate2D coordinate = {latitude , longitude};
annotation.coordinate = coordinate;
// 添加锚点
[self.mapView addAnnotation:annotation];
}
#pragma mark -自定义锚点
// MKMapViewDelegate协议中的方法,该方法的返回值可用于定制锚点控件的外观
- (MKAnnotationView *) mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>) annotation{
static NSString* annoId =@"fkAnno";

//获取可重用的锚点控件
MKAnnotationView* annoView = [mapView
dequeueReusableAnnotationViewWithIdentifier:annoId];
//如果可重用的锚点控件不存在,创建新的可重用锚点控件
if (!annoView)
{
annoView= [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annoId];
/*
如果不想改变锚点控件的图片,只想改变颜色,则可创建MKPinAnnotationView实例
再修改MKPinAnnotationView对象的pinColor属性即可。
*/
}
//为锚点控件设置图片
annoView.image = [UIImage imageNamed:@"pos.gif"];
//设置该锚点控件是否可显示气泡信息
annoView.canShowCallout =YES;
//定义一个按钮,用于为锚点控件设置附加控件
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//为按钮绑定事件处理方法
[button addTarget:self action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
// 可通过锚点控件的rightCalloutAccessoryView、leftCalloutAccessoryView设置附加控件
annoView.rightCalloutAccessoryView = button;
return annoView;

}
#pragma mark -自定义锚点 --里面的详情按钮
- (void) buttonTapped:(id)sender
{
NSLog(@"您点击了锚点信息!");
}
-(void)tapHandle:(id)sender{
QQQViewController *detailVC =[[ QQQViewController alloc]init];

[self.navigationController pushViewController:detailVC animated:YES] ;

}
![本图片表示map的xib文件](http://img.blog.csdn.net/20171105230118876?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZHVqaWFuanVucXdlcg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  模仿共享单车