之前一個月都在忙項目的事贼急,沒有時間來整理,到現(xiàn)在項目基本完成了,總結(jié)一些整合項目過程中遇到的問題赶掖。
iOS UILabel根據(jù)文本寬度改變字體大小
myLabel.adjustsFontSizeToFitWidth = YES;
假設(shè)文字內(nèi)容為@"曾在月光之下望煙花,曾共看夕陽漸降下",Label長度為200七扰,則一行顯示不下奢赂,若設(shè)置此屬性為YES,則會降低字體大小颈走,以顯示全部內(nèi)容膳灶。
如何實現(xiàn)單獨一個單元格的刷新
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
[_myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
通過獲取到當前的單元格位置,并且刷新
iOS懸停列表的實現(xiàn)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// 保證是我們的tableivew
if (scrollView == self.tableView) {
// 保證我們是垂直方向滾動立由,而不是水平滾動
if (scrollView.contentOffset.x == 0) {
CGFloat y = scrollView.contentOffset.y;
// 這個是非常關(guān)鍵的變量轧钓,用于記錄上一次滾動到哪個偏移位置
static CGFloat previousOffsetY = 0;
// 向上滾動
if (y > 0) {
if (self.headerView.bottomY <= 0) {
return;
}
// 計算兩次回調(diào)的滾動差:fabs(y - previousOffsetY)值
CGFloat bottomY = self.headerView.bottomY - fabs(y - previousOffsetY);
bottomY = bottomY >= 0 ? bottomY : 0;
self.headerView.bottomY = bottomY;
self.stickyView.y = self.headerView.bottomY;
self.tableView.frame = CGRectMake(0, self.stickyView.bottomY,
scrollView.width,
self.view.height - self.stickyView.bottomY);
previousOffsetY = y;
// 如果一直不松手滑動,重復向上向下滑動時锐膜,如果沒有設(shè)置還原為0毕箍,則會出現(xiàn)馬上到頂?shù)那闆r。
if (previousOffsetY >= self.headerView.height) {
previousOffsetY = 0;
}
}
// 向下滾動
else if (y < 0) {
if (self.headerView.y >= 0) {
return;
}
CGFloat bottomY = self.headerView.bottomY + fabs(y);
bottomY = bottomY <= self.headerView.height ? bottomY : self.headerView.height;
self.headerView.bottomY = bottomY;
self.stickyView.y = self.headerView.bottomY;
self.tableView.frame = CGRectMake(0,
self.stickyView.bottomY,
scrollView.width,
self.view.height - self.stickyView.bottomY);
}
}
}
}
將整個界面分成三個部分道盏,通過對列表滑動的y值進行判斷而柑,從而實現(xiàn)整個界面的滑動懸停效果
日期之間相互比較的方法
- (BOOL)isEqualToDate:(NSDate *)otherDate;
與otherDate比較,相同返回YES
- (NSDate *)earlierDate:(NSDate *)anotherDate;
與anotherDate比較荷逞,返回較早的那個日期
- (NSDate *)laterDate:(NSDate *)anotherDate;
與anotherDate比較媒咳,返回較晚的那個日期
- (NSComparisonResult)compare:(NSDate *)other;
該方法用于排序時調(diào)用:
當實例保存的日期值與anotherDate相同時返回NSOrderedSame
當實例保存的日期值晚于anotherDate時返回NSOrderedDescending
當實例保存的日期值早于anotherDate時返回NSOrderedAscending
實現(xiàn)類似于刮刮樂效果的核心代碼
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 觸摸任意位置
UITouch *touch = touches.anyObject;
// 觸摸位置在圖片上的坐標
CGPoint cententPoint = [touch locationInView:self.imageView];
// 設(shè)置清除點的大小
CGRect rect = CGRectMake(cententPoint.x, cententPoint.y, 20, 20);
// 默認是去創(chuàng)建一個透明的視圖 UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, NO, 0);
// 獲取上下文(畫板)
CGContextRef ref = UIGraphicsGetCurrentContext();
// 把imageView的layer映射到上下文中
[self.imageView.layer renderInContext:ref];
// 清除劃過的區(qū)域
CGContextClearRect(ref, rect);
// 獲取圖片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 結(jié)束圖片的畫板, (意味著圖片在上下文中消失)
UIGraphicsEndImageContext();
self.imageView.image = image;
在界面的同一位置放置兩張圖片,通過獲取到屏幕點擊的區(qū)域种远,將該區(qū)域的上一部分圖片清除涩澡,也就形成了類似于刮刮樂的效果。
獲取當前操作的系統(tǒng)
#define IOS_VERSION_8 ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=8)
#define IOS_VERSION_7 ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=7)