1:view中的clipsTobounds屬性
iew2添加view1到中,如果view2大于view1,或者view2的坐標不全在view1的范圍內,view2是蓋著view1的,意思就是超出的部份也會畫出來,UIView有一個屬性,clipsTobounds 默認情況下是NO。如果,我們想要view2把超出的那部份隱藏起來的話,就得改變它的父視圖也就view1的clipsTobounds屬性值。view1.clipsTobounds = YES;可以很好地解決覆蓋的問題
2:UIScrollView滾動視圖加載單元格點擊事件
svView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, mainWidth, perHeight)]; svView.bounces = NO; svView.contentSize = CGSizeMake(perWidth*6, perHeight); UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)]; [self addGestureRecognizer:tap]; [self addSubview:svView];事件:- (void)tapClick:(UITapGestureRecognizer*)tap{ CGPoint point = [tap locationInView:svView]; int page = point.x / perWidth; HomeHostest* hot = [myData objectAtIndex:page]; if(delegate) { [delegate adClick:[NSString stringWithFormat:@"goodsdetail,%@",[hot.goodsID stringValue]]]; }}- (CGPoint)locationInView:(UIView *)view:函數返回一個CGPoint類型的值,表示觸摸在view這個視圖上的位置,這里返回的位置是針對view的坐標系的。調用時傳入的view參數為空的話,返回的時觸摸點在整個窗口的位置。 (CGPoint)PReviousLocationInView:(UIView *)view:該方法記錄了前一個坐標值,函數返回也是一個CGPoint類型的值, 表示觸摸在view這個視圖上的位置,這里返回的位置是針對view的坐標系的。調用時傳入的view參數為空的話,返回的時觸摸點在整個窗口的位置。
3:對表格中的某個節跟每一行進行單獨刷新
[self.myTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
4:類似微信的搜索附近的人的功能 在 服務端的實現
基準線預先計算法每個用戶在服務器端都會保留最后的經度j1 和緯度w1 的同時 保留對經度基準線的相對距離 dj1 和緯度基準線的相對距離 dw1經度基準線可以是中國最東邊的經度 緯度基準線可以是中國最北邊的緯度 當前用戶對經度基準線的相對距離 dj2 =230km 和緯度基準線的相對距離 dw2=350km查找時sql語句可以這么寫(1.5公里內的用戶) select * from user where dj1>(230-1.5) and dj1<(230+1.5) and dw1>(230-1.5) and dw1<(230+1.5)相當于獲得當前用戶 周圍正方形1.5公里區域的用戶此外通過將冷熱數據分開存放及 分塊存放 用戶地理數據應該能有效的提高查詢速度另附:(考慮把經度和緯度分成多個表存放,比如24個經度表,每個表按緯度分別排序,查找時只在最靠近的那個表查找,由于是排序的所以并不需要遍歷每個記錄,比如緯度相差多少的就不用再找了。 )
5:定位一些基礎內容 CLLocation
01. CLLocation--------------------------------------------------------CLLocationManager 定位管理者CLLocation 代表位置(經度/緯度/高度/速度/路線等)CLHeading 代表移動方向CLRegion 代表一個區域>CLCircularRegion 圓形區域>CLBeaconRegion 藍牙信號區域返回定位服務是否可用[CLLocationManager locationServicesEnabled];返回延遲定位更新是否可用[CLLocationManager deferredLocationUpdatesAvailable];返回重大位置改變監聽是否可用[CLLocationManager significantLocationChangeMonitoringAvailable];返回是否支持磁力計算方向[CLLocationManager headingAvailable];返回藍牙信號范圍服務是否可用[CLLocationManager isRangingAvailable];--------------------------------------------------------設置是否可以暫停定位來節省電池電量, YES不需要定位數據時自動暫停定位// mgr.pausesLocationUpdatesAutomatically--------------------------------------------------------每隔多少米定位一次, 只有水平方向超過該值時才會重新定位// mgr.distanceFilter = 100;--------------------------------------------------------定位精確度// mgr.desiredAccuracy;kCLDistanceFilterNone;kCLLocationAccuracyBestForNavigation 導航級最佳精準kCLLocationAccuracyBest; 最佳精準kCLLocationAccuracyNearestTenMeters; 10米誤差kCLLocationAccuracyHundredMeters; 百米胡茬kCLLocationAccuracyKilometer; 千米誤差kCLLocationAccuracyThreeKilometers; 3千米誤差--------------------------------------------------------定位數據的用途// mgr.activityType;CLActivityTypeOther 作為普通用途CLActivityTypeAutomotiveNavigation 作為車輛導航CLActivityTypeFitness 作為步行CLActivityTypeOtherNavigation 作為其它導航--------------------------------------------------------// CLLocationlocation.coordinate; 坐標, 包含經緯度location.altitude; 設備海拔高度 單位是米location.course; 設置前進方向 0表示北 90東 180南 270西location.horizontalAccuracy; 水平精準度location.verticalAccuracy; 垂直精準度location.timestamp; 定位信息返回的時間location.speed; 設備移動速度 單位是米/秒, 適用于行車速度而不太適用于步行/* 可以設置模擬器模擬速度 bicycle ride 騎車移動 run 跑動 freeway drive 高速公路駕車 */--------------------------------------------------------// CLAuthorizationStatus用戶從未選擇過權限kCLAuthorizationStatusNotDetermined無法使用定位服務,該狀態用戶無法改變kCLAuthorizationStatusRestricted用戶拒絕該應用使用定位服務,或是定位服務總開關處于關閉狀態kCLAuthorizationStatusDenied已經授權(廢棄)kCLAuthorizationStatusAuthorized用戶允許該程序無論何時都可以使用地理信息kCLAuthorizationStatusAuthorizedAlways用戶同意程序在可見時使用地理位置kCLAuthorizationStatusAuthorizedWhenInUse--------------------------------------------------------// 計算兩個位置之間的距離, 單位是米[newLocation distanceFromLocation:self.prevLocation];--------------------------------------------------------獲取方向信息不會提示用戶(不需要授權), 因為不會泄露隱私// [self.mgr startUpdatingHeading];magneticHeading 設備與磁北的相對角度trueHeading 設置與真北的相對角度, 必須和定位一起使用, iOS需要設置的位置來計算真北真北始終指向地理北極點磁北對應隨著時間變化的地球磁場北極// 牛逼的地方116.958776,36.721199--------------------------------------------------------// 錯誤:使用CoreLocation獲取地理位置信息,報錯Error Domain=kCLErrorDomain Code=0 "The Operation couldn’t be completed. (kCLErrorDomain error 0.)"解決方法:1.確定模擬器(手機)已經聯網并且允許程序獲取地理位置2.重置地理位置服務或者網絡服務PS:如果是模擬器就果斷直接重置模擬器吧 IOS Simulator - Reset Content and Settings..。 /* 注意: iOS7只要開始定位, 系統就會自動要求用戶對你的應用程序授權. 但是從iOS8開始, 想要定位必須先"自己""主動"要求用戶授權 在iOS8中不僅僅要主動請求授權, 而且必須再info.plist文件中配置一項屬性才能彈出授權窗口 NSLocationWhenInUseDescription,允許在前臺獲取GPS的描述 NSLocationAlwaysUsageDescription,允許在后臺獲取GPS的描述 */
6:CGPoint一些常見操作
數據結構中的一個點CGPoint代表在一個二維坐標系統。數據結構的位置和尺寸CGRect代表的一個長方形。數據結構的尺寸CGSize代表寬度和高度。1、創建一個幾何原始數值CGPoint CGPointMake(CGPoint A,CGPoint B) 返回一個指定坐標點 CGRect CGRectMake(CGFloat x,CGFloat y,CGFloat width,CGFloat height)根據指定的坐標和大小創建一個矩形CGSize CGSizeMake(CGFloat width,CGFloat height)根據指定長寬創建一個CGSize 2、修改矩形CGRectDivideCGRect CGRectInset(CGRect rect,CGFloat dx,CGFloat dy)返回一個比原矩形大或小的矩形,但是中心點是相同的CGRect CGRectIntegral(CGRect A)將矩形A的值轉變成整數,得到一個最小的矩形,CGRect CGRectIntersection:(CGRect A,CGRect B)獲取兩個矩形相交處所的矩形,沒有相交返回NULL,用CGRectIsNull來檢測CGRectOffsetCGRectStandardizeCGRectUnion3、比較數值bool CGPointEqualToPoint(CGPoint A,CGPoint B) 返回兩個點是否相等bool CGSizeEqualToSize(CGSize A,CGSize B)CGSizeAB是否相等bool CGRectEqualToRect(CGRect A,CGRect B) 矩形AB的位置大小是否相等bool CGRectIntersectsRect(CGRect A,CGRect B)矩形AB是否相交,可用來判斷精靈是否離開了屏幕4、檢查bool CGRectContainsPoint(CGRect A, CGPoint B) 檢測矩形A是否包含指定的點Bbool CGRectContainsRect(CGRect A,CGRect B) 檢測矩形A是否包含矩形B5、獲取最大值、中等職和最小值CGFloat CGRectGetMinX(CGRect A) 獲取矩形x坐標的最小值CGFloat CGRectGetMinY(CGRect A)獲取矩形y坐標的最小值 CGFloat CGRectGetMidX(CGRect A) 獲取矩形x坐標的中間值 CGFloat CGRectGetMidY(CGRect A) 獲取矩形y坐標的中間值 CGFloat CGRectGetMaxX(CGRect A) 獲取矩形x坐標的最大值 CGFloat CGRectGetMaxY(CGRect A)獲取矩形y坐標的最大值 6、獲取高和寬CGFloat CGRectGetHeight(CGRect A) 獲取矩形A的高CGFloat CGRectGetWidth(CGRect A) 獲取矩形A的寬7、檢測矩形是否存在或是無窮大bool CGRectIsEmpty(CGRect A)矩形A是否長和寬都是0,或者是個NULLbool CGRectIsNull(CGRect A)矩形A是否為NULLbool CGRectIsInfinite(CGRect A)矩形A是否無窮大,沒有邊界
7:iOS7 中 boundingRectWithSize:options:attributes:context:計算文本尺寸的使用
之前使用了NSString類的sizeWithFont:constrainedToSize:lineBreakMode:方法,但是該方法已經被iOS7 Deprecated了,而iOS7新出了一個boudingRectWithSize:options:attributes:context方法來代替。而具體怎么使用呢,尤其那個attributeNSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:13]};CGSize size = [@“相關NSString” boundingRectWithSize:CGSizeMake(100, 0) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;屬性a:NSStringDrawingTruncatesLastVisibleLine:如果文本內容超出指定的矩形限制,文本將被截去并在最后一個字符后加上省略號。如果沒有指定NSStringDrawingUsesLineFragmentOrigin選項,則該選項被忽略。b:NSStringDrawingUsesLineFragmentOrigin:繪制文本時使用 line fragement origin 而不是 baseline origin。c:NSStringDrawingUsesFontLeading:計算行高時使用行距。(譯者注:字體大小+行間距=行距)d:NSStringDrawingUsesDeviceMetrics:計算布局時使用圖元字形(而不是印刷字體)。實例二: NSString *str = @"正在搜索附近的位置"; UIFont *font = [UIFont systemFontOfSize:14.0]; CGSize size = CGSizeMake(CGFLOAT_MAX, 50); CGRect rect = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:font.fontName size:font.pointSize]} context:nil]; CGRect buttonFrame = CGRectZero; buttonFrame.size.height = CGRectGetHeight(rect); buttonFrame.size.width = CGRectGetWidth(rect); UILabel *label = [[UILabel alloc]initWithFrame:buttonFrame];
8:一段布局排版關于tableView.tableFooterView設置按鍵跟等待提示
- (UIView *)searchDisplayLoadingFooterView{ if (!_searchDisplayLoadingFooterView) { _searchDisplayLoadingFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), 50)]; _searchDisplayLoadingFooterView.backgroundColor = [UIColor clearColor]; NSString *str = @"正在搜索附近的位置"; UIFont *font = [UIFont systemFontOfSize:14.0]; CGSize size = CGSizeMake(CGFLOAT_MAX, 50); CGRect rect = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:font.fontName size:font.pointSize]} context:nil]; CGRect buttonFrame = CGRectZero; buttonFrame.size.height = CGRectGetHeight(rect); buttonFrame.size.width = CGRectGetWidth(rect); UILabel *label = [[UILabel alloc]initWithFrame:buttonFrame]; label.backgroundColor = [UIColor clearColor]; label.text = str; label.font = font; label.textColor = [UIColor colorWithHexString:@"0x222222"]; label.numberOfLines = 1; label.textAlignment = NSTextAlignmentCenter; label.center = _searchDisplayLoadingFooterView.center; [_searchDisplayLoadingFooterView addSubview:label]; CGPoint indicatorCenter = CGPointZero; indicatorCenter.x = CGRectGetMinX(label.frame) - 20; indicatorCenter.y = label.center.y; UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; indicator.center = indicatorCenter; indicator.hidesWhenStopped = YES; [_searchDisplayLoadingFooterView addSubview:indicator]; [indicator startAnimating]; CGRect lineFrame = _searchDisplayLoadingFooterView.bounds; lineFrame.size.height = 0.5; UIView *topLine = [[UIView alloc]initWithFrame:lineFrame]; topLine.backgroundColor = [UIColor colorWithHexString:@"0xdddddd"]; lineFrame.origin.y = CGRectGetMaxY(_searchDisplayLoadingFooterView.bounds) - 0.5; UIView *bottomLine = [[UIView alloc]initWithFrame:lineFrame]; bottomLine.backgroundColor = [UIColor colorWithHexString:@"0xdddddd"]; [_searchDisplayLoadingFooterView addSubview:topLine]; [_searchDisplayLoadingFooterView addSubview:bottomLine]; } return _searchDisplayLoadingFooterView;}
9:百度坐標跟火星坐標相互轉換
//百度轉火星坐標+ (CLLocationCoordinate2D )bdToGGEncrypt:(CLLocationCoordinate2D)coord{ double x = coord.longitude - 0.0065, y = coord.latitude - 0.006; double z = sqrt(x * x + y * y) - 0.00002 * sin(y * M_PI); double theta = atan2(y, x) - 0.000003 * cos(x * M_PI); CLLocationCoordinate2D transformLocation ; transformLocation.longitude = z * cos(theta); transformLocation.latitude = z * sin(theta); return transformLocation;}//火星坐標轉百度坐標+ (CLLocationCoordinate2D )ggToBDEncrypt:(CLLocationCoordinate2D)coord{ double x = coord.longitude, y = coord.latitude; double z = sqrt(x * x + y * y) + 0.00002 * sin(y * M_PI); double theta = atan2(y, x) + 0.000003 * cos(x * M_PI); CLLocationCoordinate2D transformLocation ; transformLocation.longitude = z * cos(theta) + 0.0065; transformLocation.latitude = z * sin(theta) + 0.006; return transformLocation;}
新聞熱點
疑難解答