這是一個用來熟悉主流 App 架構的練習專案。整體採用 MVC 設計模式,以純程式碼為主、少量 XIB 輔助,主要完成兩件事:朋友圈動態列表,以及搖一搖搜尋。

以 UITabBarController 搭配 UINavigationController 搭出主體。實作時值得先抽出一個基類,統一管理導覽列的背景、標題樣式與 tint 顏色,以及底部標籤列的渲染方式,後續調整才不必到處改。
[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"Dimensional-_Code_Bg"] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName: [UIColor whiteColor] }];
[self.navigationBar setTintColor:[UIColor whiteColor]];
這兩頁用 UITableViewController 搭配 plist 呈現,資料來源可以是模型也可以是字典。因為邏輯單純,這裡不多著墨,只分享一個常用的字典轉模型寫法:
- (instancetype)initWithDict:(NSDictionary *)dict {
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)pictureWithDict:(NSDictionary *)dict {
return [[self alloc] initWithDict:dict];
}
這部分有三個真正需要花時間的地方:首頁的下拉動畫、選圖頁的狀態重用,以及頁面之間的資料傳遞。
下拉動畫靠的是 transform 搭配 scrollView 的幾個滾動回呼:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
self.dragging = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.num == 0) { self.num++; return; }
CGFloat offsetY = scrollView.contentOffset.y;
CGFloat angle = -offsetY * M_PI / 30;
if (self.dragging == YES) {
if (offsetY <= 110) { self.containerView.y = 10 + offsetY; }
} else {
if (self.currentY < 120) { self.containerView.y = 10 + offsetY; }
}
self.activityView.transform = CGAffineTransformMakeRotation(angle);
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
self.dragging = NO;
self.currentY = self.containerView.y;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[UIView animateWithDuration:0.25 animations:^{
self.containerView.frame = CGRectMake(15, 120, 30, 30);
self.activityView.transform = CGAffineTransformMakeRotation(2 * M_PI);
}];
}
選圖頁刻意沒有直接叫出系統相簿,而是用 UICollectionView 自製,難度因此提高不少。自訂 cell 之後,父子頁面之間透過 delegate 溝通:
@protocol YYPictureCellDelegate
@optional
- (void)pictureCell:(YYPictureCell *)cell withDidClickBtn:(UIButton *)btn;
@end
- (IBAction)clickSureBtn:(UIButton *)sender {
if ([self.delegate respondsToSelector:@selector(pictureCell:withDidClickBtn:)]) {
[self.delegate pictureCell:self withDidClickBtn:sender];
}
}
- (void)pictureCell:(YYPictureCell *)cell withDidClickBtn:(UIButton *)btn {
if ((self.selectedBtn.count == 9) && (!btn.isSelected)) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"最多選取 9 張照片喔!"
delegate:nil
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alert show];
return;
}
btn.selected = !btn.isSelected;
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
YYPictureModel *model = self.dataArray[indexPath.row];
if (btn.isSelected) {
model.clickedBtn = YES;
[self.selectedBtn addObject:btn];
[self.selImageArray addObject:model];
} else {
model.clickedBtn = NO;
[self.selectedBtn removeObject:btn];
[self.selImageArray removeObject:model];
}
BOOL hasSelection = self.selectedBtn.count > 0;
self.doneBtn.enabled = hasSelection;
self.doneBtn.selected = hasSelection;
self.previewBtn.enabled = hasSelection;
self.previewBtn.selected = hasSelection;
}
搖一搖本身只要覆寫兩個系統方法,真正的功夫在動畫。這裡的動畫並不難,麻煩的是各個控件的 frame 要一起算,稍一偏移畫面就散了。
// 搖動開始
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
self.upLine.hidden = NO;
self.downLine.hidden = NO;
[UIView animateWithDuration:0.6 animations:^{
self.upImageView.y -= 60;
self.upLine.y -= 60;
self.downImageView.y += 60;
self.downLine.y += 60;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.6 animations:^{
self.upImageView.y += 60;
self.upLine.y += 60;
self.downImageView.y -= 60;
self.downLine.y -= 60;
} completion:^(BOOL finished) {
self.upLine.hidden = YES;
self.downLine.hidden = YES;
// 動畫收尾後彈出搜尋框,並在稍後自動移除
[self showSearchView];
[self.searchView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2.4];
}];
}];
}
// 搖動結束
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
}
把這兩段功能做出來,大致就摸過一遍主流 App 常見的骨架:分頁導航、列表渲染、自訂動畫與跨頁通訊。之後要接上真實的網路請求,替換掉本地 plist 即可。