iOS开发技巧iOSIOS 技巧功能案例

iOS开发—技巧总结(一)

2016-06-15  本文已影响1002人  Jeavil_Tang

今天心血来潮看了看几天前收藏的别人家的代码,学到一些小技巧,迫不及待想总结一下,以备不时之需。
分享一个原作者的博客地址: http://www.jianshu.com/p/6decbc69c197正如作者所说的这个Demo里面有很多好东西值得学习哦!

First Tip

CGAffineTransformIdentity

先粘两段代码:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [UIView animateWithDuration:1 animations:^{
        self.tabBarController.tabBar.transform = CGAffineTransformMakeTranslation(0, 49);
    }];
}

上面是 ScrollView 的代理方法,当将要开始滑动 tableview 的时候平移 tabBar,我建议大家可以普及一下关于 CGAffineTransform的知识,还是蛮有用的。

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [UIView animateWithDuration:1.0 delay:0.8 options:UIViewAnimationOptionOverrideInheritedCurve animations:^{
        self.tabBarController.tabBar.transform = CGAffineTransformIdentity;
    } completion:nil];
}

上面一段代码时已经结束滑动 tableview 的时候所做的动画,重点就是这行代码 self.tabBarController.tabBar.transform = CGAffineTransformIdentity; 将 tabBar 恢复到原始咩有 CGAffineTransform 运行过的状态,也就是让View回到原始状态。

关于 View.transform = CGAffineTransformIdentity .
如果我们在为一个view设置了多个CGAffineTransform, 那么每一个CGAffineTransform都以在上一个CGAffineTransform运行完后的位置的center作为参照点运行的.
如果我们在每一个CGAffineTransform运行前加一句:
View.transform = CGAffineTransformIdentity
那么会先把view恢复到原始的没有CGAffineTransform运行过得状态, 然后再运行CGAffineTransform.
这样就相当于, 每一个CGAffineTransform运行前, view都会先归位. 然后再运行. 所以没有CGAffineTransform都是以view的原始位置为参考, 互不影响.
我们也可以在进行了一系列CGAffineTransform后 通过View.transform = CGAffineTransformIdentity 来让view回到原始状态
原文地址:http://www.dahuangphone.com/dv_rss.asp?s=xhtml&boardid=8&id=274&page=2

Second Tip

拖动 scollview 过程中实现渐变效果

来献上代码:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UIColor *color = [UIColor brownColor];
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY > 0) {
        CGFloat alpha = 1 - ((64 - offsetY) / 64);
        if (alpha>1) {
            alpha = 1;
        }
        _topBgView.backgroundColor = [color colorWithAlphaComponent:alpha];
    } else {
        _topBgView.backgroundColor = [color colorWithAlphaComponent:0];
    }
}

在拖动 scrollView 的过程中,均匀改变 _topBgView 的背景色,很简单的实现思路,但有两点值得一提:

Third Tip

当一个 tableview 的headerView 或footerView 有点击加载更多更能的时候,可以在 tableview 的代理方法实现 - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
方法
下面方法是点击tableview 每个section 的 headerview 时加载更多的信息

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    MGHeadView *sectionHeadView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headViewIdentifier];
    
    MGSectionModel *sectionModel = self.sectionDataSources[section];
    sectionHeadView.model = sectionModel;
    
    sectionHeadView.expandCallback = ^(BOOL isExpanded){
        [tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
    };
    
    return sectionHeadView;
}
上一篇下一篇

猜你喜欢

热点阅读