iOS项目总结

之前一个月都在忙项目的事,没有时间来整理,到现在项目基本完成了,总结一些整合项目过程中遇到的问题。

iOS UILabel根据文本宽度改变字体大小

myLabel.adjustsFontSizeToFitWidth = YES;
假设文字内容为@"曾在月光之下望烟花,曾共看夕阳渐降下",Label长度为200,则一行显示不下,若设置此属性为YES,则会降低字体大小,以显示全部内容。

如何实现单独一个单元格的刷新

        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
        [_myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];

通过获取到当前的单元格位置,并且刷新

iOS悬停列表的实现

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    // 保证是我们的tableivew

    if (scrollView == self.tableView) {

        // 保证我们是垂直方向滚动,而不是水平滚动

        if (scrollView.contentOffset.x == 0) {

            CGFloat y = scrollView.contentOffset.y;

            // 这个是非常关键的变量,用于记录上一次滚动到哪个偏移位置
            static CGFloat previousOffsetY = 0;
            // 向上滚动
               if (y > 0) {

                if (self.headerView.bottomY <= 0) {

                    return;

                }

                

                // 计算两次回调的滚动差: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;

                

                // 如果一直不松手滑动,重复向上向下滑动时,如果没有设置还原为0,则会出现马上到顶的情况。

                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值进行判断,从而实现整个界面的滑动悬停效果

日期之间相互比较的方法

- (BOOL)isEqualToDate:(NSDate *)otherDate;
与otherDate比较,相同返回YES
- (NSDate *)earlierDate:(NSDate *)anotherDate;
与anotherDate比较,返回较早的那个日期 
- (NSDate *)laterDate:(NSDate *)anotherDate;
与anotherDate比较,返回较晚的那个日期
- (NSComparisonResult)compare:(NSDate *)other;
 该方法用于排序时调用:
 当实例保存的日期值与anotherDate相同时返回NSOrderedSame
 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending
 当实例保存的日期值早于anotherDate时返回NSOrderedAscending

实现类似于刮刮乐效果的核心代码

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
 // 触摸任意位置
 UITouch *touch = touches.anyObject;
 // 触摸位置在图片上的坐标
 CGPoint cententPoint = [touch locationInView:self.imageView];
 // 设置清除点的大小
 CGRect  rect = CGRectMake(cententPoint.x, cententPoint.y, 20, 20);
 // 默认是去创建一个透明的视图     UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, NO, 0);  
 // 获取上下文(画板)
 CGContextRef ref = UIGraphicsGetCurrentContext();
    // 把imageView的layer映射到上下文中
 [self.imageView.layer renderInContext:ref];
 // 清除划过的区域
 CGContextClearRect(ref, rect);  
 // 获取图片
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 // 结束图片的画板, (意味着图片在上下文中消失)
 UIGraphicsEndImageContext();
 self.imageView.image = image;

在界面的同一位置放置两张图片,通过获取到屏幕点击的区域,将该区域的上一部分图片清除,也就形成了类似于刮刮乐的效果。

获取当前操作的系统

#define IOS_VERSION_8  ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=8)

#define IOS_VERSION_7  ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=7)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容