搖一搖根據城市位置推薦酒店

搖一搖根據城市位置推薦酒店客戶端

1、實現搖一搖並震動需要導入頭文件。#import

[self becomeFirstResponder];
[UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;

4、關鍵代碼
在移動的時候將手機震動,並將view顯示出來,並請求接口,將酒店顯示出來,點擊進入到酒店詳情界面。

#pragma mark - UIResponder
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
self.bgView.alpha = 0;
self.bgView.hidden = YES;
[UIView animateWithDuration:1.0 animations:^{
[self getRandomHotel];
self.bgView.alpha = 1;
self.bgView.hidden = NO;
self.hotelImage.image = [UIImage imageNamed:@"My_about"];
self.label.text = @"您已經成功搖到一個酒店,不喜歡?換個姿勢再來一次";
[self.label sizeToFit];
self.hotelImage.contentMode = UIViewContentModeScaleAspectFill;
self.hotelImage.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.hotelImage.clipsToBounds = YES;
//動態設置uilabel的高度
self.hotelLabel.numberOfLines = 0;
self.hotelLabel.lineBreakMode = NSLineBreakByWordWrapping;
} completion:^(BOOL finished) {

}];
NSLog(@"搖一搖開始");
return ;
}

-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"取消搖一搖");
return ;
}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
if (motion ==UIEventSubtypeMotionShake ){
NSLog(@"搖一搖結束");
}
return ;
}

全部代碼:


//
// ShakeViewController.m
// 住哪兒
//
// Created by geek on 2017/4/30.
// Copyright © 2017年 geek. All rights reserved.
//

#import "ShakeViewController.h"
#import "HotelDetailVC.h"
#import "HotelsModel.h"
#import "LocationManager.h"
#import <AudioToolbox/AudioToolbox.h>

@interface ShakeViewController ()<LocationManagerDelegate>
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *hotelLabel;
@property (nonatomic, strong) UIImageView *hotelImage;
@property (nonatomic, strong) HotelsModel *model;
@property (nonatomic, strong) LocationManager *locationManager;
@property (nonatomic, strong) NSString *cityName;
@property (nonatomic, assign) BOOL showHotel;
@end

@implementation ShakeViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self setupUI];
[self autoLocate];
}

- (void)setupUI {
[self.view addSubview:self.imageView];
[self becomeFirstResponder];
[UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
[self.view addSubview:self.label];
[self.view addSubview:self.bgView];
[self.bgView addSubview:self.hotelImage];
[self.bgView addSubview:self.hotelLabel];
}

-(void)autoLocate{
self.locationManager = [LocationManager sharedInstance];
self.locationManager.delegate = self;
[self.locationManager autoLocate];
}

-(void)getRandomHotel{
NSString *url = [NSString stringWithFormat:@"%@%@",Base_Url,@"/controller/api/RandomHotel.php"];
NSMutableDictionary *paras = [NSMutableDictionary dictionary];
paras[@"key"] = AppKey;
paras[@"city"] = self.cityName;
[SVProgressHUD showWithStatus:@"正在獲取酒店數據"];
[AFNetPackage getJSONWithUrl:url parameters:paras success:^(id responseObject) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
if ([dic[@"code"] integerValue] == 200) {
[SVProgressHUD dismiss];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
self.model = [HotelsModel yy_modelWithJSON:dic[@"data"]];
self.hotelLabel.text = self.model.hotelName;
[self.hotelLabel sizeToFit];
[self.hotelImage sd_setImageWithURL:[NSURL URLWithString: [NSString stringWithFormat:@"%@/%@",Base_Url,self.model.image1]] placeholderImage:[UIImage imageNamed:@"jpg-1"]];
}
} fail:^{
[SVProgressHUD dismiss];
}];
}

-(void)watchDetail{
HotelDetailVC *vc = [[HotelDetailVC alloc] init];
vc.startPeriod = [[NSDate date] todayString];
vc.leavePerios = [[NSDate date] GetTomorrowDayString];
vc.model = self.model;
[self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - LocationManagerDelegate
-(void)locationManager:(LocationManager *)locationManager didGotLocation:(NSString *)location{
self.cityName = location;
}

#pragma mark - UIResponder
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
self.bgView.alpha = 0;
self.bgView.hidden = YES;
[UIView animateWithDuration:1.0 animations:^{
[self getRandomHotel];
self.bgView.alpha = 1;
self.bgView.hidden = NO;
self.hotelImage.image = [UIImage imageNamed:@"My_about"];
self.label.text = @"您已經成功搖到一個酒店,不喜歡?換個姿勢再來一次";
[self.label sizeToFit];
self.hotelImage.contentMode = UIViewContentModeScaleAspectFill;
self.hotelImage.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.hotelImage.clipsToBounds = YES;
//動態設置uilabel的高度
self.hotelLabel.numberOfLines = 0;
self.hotelLabel.lineBreakMode = NSLineBreakByWordWrapping;
} completion:^(BOOL finished) {

}];
NSLog(@"搖一搖開始");
return ;
}

-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"取消搖一搖");
return ;
}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
if (motion ==UIEventSubtypeMotionShake ){
NSLog(@"搖一搖結束");
}
return ;
}

#pragma mark - lazy load
-(UIImageView *)imageView{
if (!_imageView) {
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, BoundWidth, BoundHeight-60)];
_imageView.image = [UIImage imageNamed:@"shake_news_bgVPic"];
}
return _imageView;
}

-(UILabel *)label{
if (!_label) {
_label = [[UILabel alloc] initWithFrame:CGRectMake(BoundWidth/2-200/2, BoundHeight - 60 -270, 200, 21)];
_label.textColor = [UIColor whiteColor];
_label.numberOfLines = 2;
_label.font = [UIFont systemFontOfSize:15];
_label.text = @"搖一搖,爲您隨機推薦酒店";
[_label sizeToFit];
}
return _label;
}

-(UIView *)bgView{
if (!_bgView) {
_bgView = [[UIView alloc] initWithFrame:CGRectMake(15, BoundHeight-260, BoundWidth-30, 120)];
_bgView.backgroundColor = [UIColor whiteColor];
_bgView.layer.cornerRadius = 10;
_bgView.clipsToBounds = YES;
_bgView.alpha = 0;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(watchDetail)];
tap.cancelsTouchesInView = YES;
_bgView.userInteractionEnabled = YES;
[_bgView addGestureRecognizer:tap];
}
return _bgView;
}

-(UIImageView *)hotelImage{
if (!_hotelImage) {
_hotelImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
_hotelImage.contentMode = UIViewContentModeScaleAspectFit;
}
return _hotelImage;
}

-(UILabel *)hotelLabel{
if (!_hotelLabel) {
_hotelLabel = [[UILabel alloc] initWithFrame:CGRectMake(135, 40, BoundWidth - 30- 140, 40)];
_hotelLabel.textColor = [UIColor blackColor];
_hotelLabel.font = [UIFont systemFontOfSize:20];
}
return _hotelLabel;
}
@end

效果圖
搖一搖之前

搖一搖搖到酒店

隨機推薦酒店後臺實現

實現思路:

1、接收客戶端傳來的參數:城市名稱、App key;

2、根據城市名稱模糊搜索出酒店數據;

3、根據搜索出的酒店數據數組長度,生成1個隨機數,隨機數範圍[1,數組長度];

4、利用封裝好的Responese將數據返給客戶端

<?php

/**
* Created by PhpStorm.
* User: geek
* Date: 2017/3/9
* Time: 上午9:24
*/

header('content-type:text.html;charset=utf-8');
error_reporting(0);
require_once '../../model/PdoMySQL.class.php';
require_once '../../model/config.php';
require_once 'Response.php';


class RandomHotel
{
private $tableName = "hotel";
private $key = "";
private $city = "";

protected static $_instance = null;

private function __construct()
{
}

private function __clone()
{
// TODO: Implement __clone() method.
}

public function sharedInstance(){
if(self::$_instance == null){
self::$_instance = new self();
}
return self::$_instance;
}



private function random($start,$end){
$tmp=array();
while(count($tmp)<1){
$tmp[]=mt_rand($start,$end);
$tmp=array_unique($tmp);
}
return $tmp[0]-1;
}

public function getHotel(){
$mysqlPdo = new PdoMySQL();

self.$this->key = $_REQUEST["key"];
self.$this->city = $_REQUEST["city"];

if($this->key == "" || $this->key !== "TheHotelReversationApplication" ){
Response::show(201,"fail","非安全的數據請求","json");
}

$pdo=new PdoMySQL();
$res = $pdo->find($this->tableName,"address like '%".$this->city."%'");

$random = $this->random(1,count($res));

if($res){
//隨機酒店獲取成功
Response::show(200,"隨機酒店獲取成功",$res[$random],"json");
}else{
//隨機酒店獲取失敗
Response::show(201,"隨機酒店獲取失敗","json");
}
}
}

$hotel = RandomHotel::sharedInstance();
$hotel->getHotel();
?>

想看完整項目獻上傳送門:住哪兒客戶端住哪兒服務端

發佈了42 篇原創文章 · 獲贊 6 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章