ViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // ViewController.m
  3. // Gomoku
  4. //
  5. // Created by Sekorm on 16/7/25.
  6. // Copyright © 2016年 HY. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. #import "CheckerboardView.h"
  10. #define ScreenW [UIScreen mainScreen].bounds.size.width
  11. @interface ViewController ()<CLLocationManagerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate>
  12. @property (nonatomic,weak) CheckerboardView * boardView;
  13. @property (nonatomic,weak) UIButton * backButton;
  14. @property (nonatomic,weak) UIButton * reStartBtn;
  15. @property (nonatomic,weak) UIButton * changeBoardButton;
  16. @property(nonatomic, strong) UILabel *info;
  17. @property(nonatomic, strong) CLLocationManager *locationManager;
  18. @property(nonatomic, strong) UIImageView *backImageView;
  19. @end
  20. @implementation ViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. [self setUp];
  24. [self startJJJ];
  25. }
  26. - (void)setUp{
  27. self.view.backgroundColor = [UIColor colorWithWhite:1 alpha:0.8];
  28. _backImageView = [[UIImageView alloc] init];
  29. _backImageView.frame = CGRectMake(0, 0, kScreenW, kMainScreenHeight);
  30. [self.view addSubview:_backImageView];
  31. //添加棋盘
  32. CheckerboardView * boardView = [[CheckerboardView alloc]initWithFrame:CGRectMake(20, 30, ScreenW * 0.95, CGFLOAT_MAX)];
  33. boardView.center = self.view.center;
  34. [self.view addSubview:boardView];
  35. self.boardView = boardView;
  36. //悔棋
  37. UIButton * changeBoardButton = [UIButton buttonWithType:UIButtonTypeCustom];
  38. [changeBoardButton setTitle:ASLocalizedString(@"初级棋盘")forState:UIControlStateNormal];
  39. [changeBoardButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
  40. changeBoardButton.backgroundColor = [UIColor colorWithRed:200/255.0 green:160/255.0 blue:130/255.0 alpha:1];
  41. changeBoardButton.frame = CGRectMake(CGRectGetMidX(boardView.frame) - CGRectGetWidth(boardView.frame) * 0.3, CGRectGetMinY(boardView.frame) - 50, CGRectGetWidth(boardView.frame) * 0.6, 35);
  42. changeBoardButton.layer.cornerRadius = 4;
  43. [self.view addSubview:changeBoardButton];
  44. self.changeBoardButton = changeBoardButton;
  45. [changeBoardButton addTarget:self action:@selector(changeBoard:) forControlEvents:UIControlEventTouchUpInside];
  46. //悔棋
  47. UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
  48. [backButton setTitle:ASLocalizedString(@"悔棋")forState:UIControlStateNormal];
  49. [backButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
  50. backButton.backgroundColor = [UIColor colorWithRed:200/255.0 green:160/255.0 blue:130/255.0 alpha:1];
  51. backButton.frame = CGRectMake(CGRectGetMinX(boardView.frame), CGRectGetMaxY(boardView.frame) + 15, CGRectGetWidth(boardView.frame) * 0.45, 30);
  52. backButton.layer.cornerRadius = 4;
  53. [self.view addSubview:backButton];
  54. self.backButton = backButton;
  55. [backButton addTarget:self action:@selector(backOneStep:) forControlEvents:UIControlEventTouchUpInside];
  56. //新游戏
  57. UIButton * reStartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  58. [reStartBtn setTitle:ASLocalizedString(@"新游戏")forState:UIControlStateNormal];
  59. reStartBtn.backgroundColor = [UIColor colorWithRed:200/255.0 green:160/255.0 blue:130/255.0 alpha:1];
  60. reStartBtn.frame = CGRectMake(CGRectGetMaxX(boardView.frame) - CGRectGetWidth(boardView.frame) * 0.45, CGRectGetMaxY(boardView.frame) + 15, CGRectGetWidth(boardView.frame) * 0.45, 30);
  61. reStartBtn.layer.cornerRadius = 4;
  62. [self.view addSubview:reStartBtn];
  63. self.reStartBtn = reStartBtn;
  64. [reStartBtn addTarget:self action:@selector(newGame) forControlEvents:UIControlEventTouchUpInside];
  65. //游戏背景
  66. UIButton * gameBg = [UIButton buttonWithType:UIButtonTypeCustom];
  67. [gameBg setTitle:ASLocalizedString(@"设置游戏背景")forState:UIControlStateNormal];
  68. gameBg.backgroundColor = [UIColor colorWithRed:200/255.0 green:160/255.0 blue:130/255.0 alpha:1];
  69. gameBg.frame = CGRectMake(CGRectGetMaxX(boardView.frame) - CGRectGetWidth(boardView.frame) * 0.45, CGRectGetMaxY(reStartBtn.frame) + 15, CGRectGetWidth(boardView.frame) * 0.45, 30);
  70. gameBg.layer.cornerRadius = 4;
  71. [self.view addSubview:gameBg];
  72. self.reStartBtn = gameBg;
  73. [gameBg addTarget:self action:@selector(setGameBg) forControlEvents:UIControlEventTouchUpInside];
  74. _info = [[UILabel alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight + kStatusBarHeight, kScreenW, 40)];
  75. _info.text = ASLocalizedString(@"没有位置信息");
  76. [self.view addSubview:_info];
  77. }
  78. - (void)backOneStep:(UIButton *)sender{
  79. [self.boardView backOneStep:(UIButton *)sender];
  80. }
  81. - (void)newGame{
  82. [self.boardView newGame];
  83. }
  84. - (void)changeBoard:(UIButton *)btn{
  85. [self.boardView changeBoardLevel];
  86. [_changeBoardButton setTitle:[btn.currentTitle isEqualToString:ASLocalizedString(@"高级棋盘")]?ASLocalizedString(@"初级棋盘"):ASLocalizedString(@"高级棋盘")forState:UIControlStateNormal];
  87. }
  88. -(void)startJJJ
  89. {
  90. [self startLocation];
  91. }
  92. -(void)startLocation{
  93. if ([CLLocationManager locationServicesEnabled]) {//判断定位操作是否被允许
  94. self.locationManager = [[CLLocationManager alloc] init];
  95. self.locationManager.delegate = self;//遵循代理
  96. self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  97. self.locationManager.distanceFilter = 10.0f;
  98. [_locationManager requestWhenInUseAuthorization];//使用程序其间允许访问位置数据(iOS8以上版本定位需要)
  99. [self.locationManager startUpdatingLocation];//开始定位
  100. }else{//不能定位用户的位置的情况再次进行判断,并给与用户提示
  101. //1.提醒用户检查当前的网络状况
  102. //2.提醒用户打开定位开关
  103. [[BGHUDHelper sharedInstance] syncStopLoading];
  104. }
  105. }
  106. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
  107. //当前所在城市的坐标值
  108. CLLocation *currLocation = [locations lastObject];
  109. NSLog(ASLocalizedString(@"经度=%f 纬度=%f 高度=%f"), currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);
  110. //根据经纬度反向地理编译出地址信息
  111. CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
  112. [geoCoder reverseGeocodeLocation:currLocation completionHandler:^(NSArray *placemarks, NSError *error) {
  113. for (CLPlacemark * placemark in placemarks) {
  114. NSDictionary *address = [placemark addressDictionary];
  115. // Country(国家) State(省) City(市)
  116. NSLog(@"#####%@",address);
  117. NSLog(@"%@", [address objectForKey:@"Country"]);
  118. NSLog(@"%@", [address objectForKey:@"State"]);
  119. NSLog(@"%@", [address objectForKey:@"City"]);
  120. _info.text = [NSString stringWithFormat:ASLocalizedString(@"欢迎来自%@的棋手"),[address objectForKey:@"City"]];
  121. // cell.rightLab.text = [address objectForKey:@"City"];
  122. [[BGHUDHelper sharedInstance] syncStopLoading];
  123. }
  124. }];
  125. }
  126. -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
  127. [[BGHUDHelper sharedInstance] syncStopLoading];
  128. if ([error code] == kCLErrorDenied){
  129. //访问被拒绝
  130. [FanweMessage alertHUD:ASLocalizedString(@"访问被拒绝")];
  131. }
  132. if ([error code] == kCLErrorLocationUnknown) {
  133. //无法获取位置信息
  134. [FanweMessage alertHUD:ASLocalizedString(@"无法获取位置信息")];
  135. }
  136. }
  137. - (void)setGameBg {
  138. [self clickHeadImage];
  139. }
  140. //编辑公会头像
  141. -(void)clickHeadImage
  142. {
  143. UIActionSheet *headImgSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
  144. [headImgSheet addButtonWithTitle:ASLocalizedString(@"相机")];
  145. [headImgSheet addButtonWithTitle:ASLocalizedString(@"从手机相册选择")];
  146. [headImgSheet addButtonWithTitle:ASLocalizedString(@"取消")];
  147. headImgSheet.cancelButtonIndex = headImgSheet.numberOfButtons-1;
  148. headImgSheet.delegate = self;
  149. [headImgSheet showInView:[UIApplication sharedApplication].keyWindow];
  150. }
  151. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  152. {
  153. if (buttonIndex == 0)
  154. {
  155. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  156. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  157. {
  158. picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  159. picker.delegate = self;
  160. picker.allowsEditing = YES;
  161. picker.modalTransitionStyle = UIModalPresentationOverCurrentContext;
  162. }
  163. [self presentViewController:picker animated:YES completion:nil];
  164. }else if (buttonIndex == 1){
  165. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  166. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
  167. {
  168. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  169. picker.delegate = self;
  170. picker.allowsEditing = YES;
  171. picker.modalPresentationStyle = UIModalPresentationOverCurrentContext;
  172. }
  173. [self presentViewController:picker animated:YES completion:nil];
  174. }
  175. }
  176. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
  177. UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
  178. // [self.headBtn setImage:image forState:UIControlStateNormal];
  179. _backImageView.image = image;
  180. [picker dismissViewControllerAnimated:YES completion:nil];
  181. }
  182. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  183. {
  184. [self dismissViewControllerAnimated:YES completion:^{}];
  185. }
  186. //点击返回按钮
  187. - (void)comeBack
  188. {
  189. [self.navigationController popViewControllerAnimated:NO];
  190. }
  191. @end