iOS开发札记

2019-03-07:iOS开发札记

2019-03-08  本文已影响0人  QxyBest

知识点:

  1. UIApplication.shared.isNetworkActivityIndicatorVisible = true
    是否在顶部状态栏显示网络状态(转圈圈表示网络未接入)
  2. 在编辑器添加UIColor 时可以选择自动补全的 color literal,就可以打开颜色编辑器了

代码片段

用代码定义有事件的按钮:

btnAdd.addTarget(self, action: #selector(addView(_:)), for: .touchUpInside)

@objc func addView(_ sender:UIButton){
        let rectView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        rectView.backgroundColor = UIColor.green
        
        view.addSubview(rectView)
}

给View添加边框效果:

btnAdd.layer.borderWidth = 10
btnAdd.layer.borderColor = UIColor.red.cgColor
btnAdd.layer.cornerRadius = 20
btnAdd.layer.masksToBounds = true

给VIew添加阴影效果:

imageView.layer.shadowColor = UIColor.gray.cgColor
imageView.layer.shadowOffset = CGSize(width: 10, height: 10)
imageView.layer.shadowOpacity = 0.5
imageView.layer.shadowRadius = 10

给View添加渐变颜色:

        let gradienLayer = CAGradientLayer()
        gradienLayer.frame = imageView.frame
        
        let redColor = UIColor.red.cgColor
        let greenColor = UIColor.green.cgColor
        let blueColor = UIColor.blue.cgColor
        
        gradienLayer.colors = [redColor,blueColor,greenColor]
        
        imageView.layer.addSublayer(gradienLayer)

给View类设置纹理填充

        let image = UIImage(named: "paper")
        let patternColor = UIColor(patternImage: image!)
        imageView.backgroundColor = patternColor

给View类设置点击事件(单点,双点,长按)

        let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))
        
        let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(doubleTap(_:)))
        doubleTapGesture.numberOfTouchesRequired = 1
        doubleTapGesture.numberOfTapsRequired = 2
        
        imageView.isUserInteractionEnabled = true
        
        imageView.addGestureRecognizer(longPressGesture)
        imageView.addGestureRecognizer(doubleTapGesture)
        
        view.addSubview(imageView)

    @objc func longPress(_ gesture : UILongPressGestureRecognizer){
        if gesture.state == .began{
            let alert = UIAlertController(title: "Information", message: "Long Message", preferredStyle: .alert)
            let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alert.addAction(OKAction)
            self.present(alert, animated: true, completion: nil)
        }
    }
    
    @objc func doubleTap(_ gesture : UITapGestureRecognizer){
        let alert = UIAlertController(title: "Information", message: "Double Tap", preferredStyle: .alert)
        let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alert.addAction(OKAction)
        self.present(alert, animated: true, completion: nil)
    }
上一篇下一篇

猜你喜欢

热点阅读