churabou / iOS-develop-blog

0 stars 0 forks source link

4月26日(木) これから #12

Open churabou opened 6 years ago

churabou commented 6 years ago

自分にとってプログラミングがお金を稼ぐための手段でしかないことはハッキリしている。

情報を取得する力


情報を整理する力


1度手に入れたものを2度と失わない。

churabou commented 6 years ago

結局このExtension便利


extension CGSize {

    mutating func translatedBy(dw: CGFloat, dh: CGFloat) {
        self.width += dw
        self.height += dh
    }

    mutating func scale(by: CGFloat) {
        self.width *= by
        self.height *= by
    }
}

extension CGSize {
    static func *(lhs: CGSize, rhs: CGFloat) -> CGSize {
        return CGSize(width: lhs.width * rhs, height: lhs.height * rhs)
    }
}

extension CGPoint {

    mutating func translatedBy(dx: CGFloat, dy: CGFloat) {
        self.x += dx
        self.y += dy
    }
}
churabou commented 6 years ago
extension UIView {
    func mask(withRect rect: CGRect, inverse: Bool = false) {
        let path = UIBezierPath(rect: rect)
        let maskLayer = CAShapeLayer()

        if inverse {
            path.append(UIBezierPath(rect: bounds))
            maskLayer.fillRule = kCAFillRuleEvenOdd
        }

        maskLayer.path = path.cgPath

        layer.mask = maskLayer
    }
}
churabou commented 6 years ago

でっかいViewを背後で動かして擬似的に これ以上悩んでも不毛なので

質問として残す、

更新後


class ViewController: UIViewController {

    private lazy var imageView: UIView = {
        let v = UIView()
        v.backgroundColor = .red
        v.frame = view.bounds.insetBy(dx: 50, dy: 150)
        return v
    }()

    private lazy var maskLayer: CAShapeLayer = {
        let l = CAShapeLayer()
        l.fillRule = kCAFillRuleEvenOdd
        l.fillColor = UIColor.orange.cgColor //alphaが1ならなんでも
        return l
    }()

    private lazy var maskView: UIView = {
        let v = UIView()

        v.bounds.size = imageView.bounds.size * 3
        v.center = imageView.bounds.center
        v.backgroundColor = .black
        v.addGestureRecognizer(pan)
        v.layer.mask = maskLayer

        let center = v.bounds.center
        let maskPath = UIBezierPath(rect: v.bounds)
        let r: CGFloat = 50
        maskPath.append(UIBezierPath(ovalIn: CGRect(x: center.x-r,
                                                    y: center.y-r,
                                                    width: 2*r,
                                                    height: 2*r)))
        maskLayer.path = maskPath.cgPath
        return v
    }()

    private lazy var pan: UIPanGestureRecognizer = {
        let p = UIPanGestureRecognizer(target: self, action: #selector(move))
        return p
    }()

    private var pre: CGPoint = .zero
    @objc func move(_ sender: UIPanGestureRecognizer) {
        let location = sender.location(in: imageView)
        if sender.state == .began {
            pre = location
        }
        let dx = location.x - pre.x
        let dy = location.y - pre.y

        pre = location

        maskView.center.x += dx
        maskView.center.y += dy
    }

    override func viewDidLoad() {
        imageView.clipsToBounds = true
        view.addSubview(imageView)
        imageView.addSubview(maskView)
    }
}