美文网首页
Swift——项目中遇到的问题<三>

Swift——项目中遇到的问题<三>

作者: 玉思盈蝶 | 来源:发表于2020-09-04 18:36 被阅读0次

1、Swift 设置阴影:

参考链接:

https://www.jianshu.com/p/5886bfbc5734

2、iOS scrollView不能滑动:

问题描述:

在使用masonry进行界面布局时,把scrollView添加到view上发现不能滑动。

原因:

没有设置scrollView父视图的高度,滑动位置超过了父视图的区域,导致不能滑动。

补充:当然scrollView的contentSize要比frame大

3、iOS 脚本查看项目中未使用的类:

https://www.jianshu.com/p/de03ea15f399?utm_source=desktop&utm_medium=timeline

4、真机调试添加设备文件:

https://github.com/iGhibli/iOS-DeviceSupport
强烈推荐收藏哈~~~

5、效果实现:

image.png

UIlabel + UIScrollView + 阴影View + UIButton
阴影View加载scrollView的尾部即可。

一开始打算自己写阴影,最后还是偷懒切图了,哈哈。

6、iOS界面滑动卡顿优化:

测试反馈滑动切换有卡顿问题,同事分析有可能是我滑动切换的时候赋值数据都是先移除之前的子视图,再创建添加新视图的问题导致的。话说滑动54的fps也能看出卡顿吗,哈哈,到底fps多少算可以呢?

可以使用Instruments工具分析哈。
image.png

分析大概还是能看到问题在哪的哈,下图所示。可以看到具体哪个方法耗时,再优化即可。


image.png

我暂时的处理是每次滑动切换不移除重新创建了,再每次cell赋值的时候赋值创建子视图,滑动切换的时候隐藏其他的cell子视图,显示当前cell子视图即可,不做移除操作。fps确实达到了60,暂时就这样吧~~

Swift版FPSLabel:

import UIKit

class FPSLabel: UILabel {
    fileprivate var _link :CADisplayLink?
    fileprivate var _count:Int = 0
    fileprivate var _lastTime:TimeInterval = 0

    fileprivate let _defaultSize = CGSize(width: 55, height: 20);
    
    override init(frame: CGRect) {
        var targetFrame = frame
        if frame.size.width == 0 && frame.size.height == 0{
            targetFrame.size = _defaultSize
        }
        super.init(frame: targetFrame)
        self.layer.cornerRadius = 5
        self.clipsToBounds = true
        self.textAlignment = .center
        self.isUserInteractionEnabled = false
        self.textColor = UIColor.white
        self.backgroundColor = UIColor(white: 0, alpha: 0.7)
        self.font = UIFont(name: "Menlo", size: 14)
        weak var weakSelf = self
        _link = CADisplayLink(target: weakSelf!, selector:#selector(FPSLabel.tick(_:)) );
        _link!.add(to: RunLoop.main, forMode:RunLoop.Mode.common)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    @objc func tick(_ link:CADisplayLink) {
        if _lastTime == 0  {
            _lastTime = link.timestamp
            return
        }
        
        _count += 1
        let delta = link.timestamp - _lastTime
        if delta < 1 {
            return
        }
        _lastTime = link.timestamp
        let fps = Double(_count) / delta
        _count = 0
        
        let progress = fps / 60.0;
        self.textColor = UIColor(hue: CGFloat(0.27 * ( progress - 0.2 )) , saturation: 1, brightness: 0.9, alpha: 1)
        self.text = "\(Int(fps+0.5))FPS"
        
    }
}
参考链接:
Instruments性能优化-Core Animation:

http://www.cocoachina.com/articles/18341

iOS - fps 60 优化:

https://www.cnblogs.com/baitongtong/p/13403849.html

PS:大家周末快乐呀!!!

相关文章

网友评论

      本文标题:Swift——项目中遇到的问题<三>

      本文链接:https://www.haomeiwen.com/subject/smktektx.html