churabou / iOS-develop-blog

0 stars 0 forks source link

4月30日 (月) #16

Open churabou opened 6 years ago

churabou commented 6 years ago

あんま読んでない 結局ここっから

class MaskView: BaseView {

    override class var layerClass: AnyClass {
        return CAShapeLayer.self
    }

    override func initializeView() {

        let layer = self.layer as! CAShapeLayer
        layer.fillRule = kCAFillRuleEvenOdd
        layer.fillColor = UIColor.orange.cgColor
    }

    override func draw(_ rect: CGRect) {

        let path = UIBezierPath(rect: bounds)

        let center = bounds.center
        let r: CGFloat = 50
        path.append(UIBezierPath(ovalIn: CGRect(x: center.x-r,
                                                    y: center.y-r,
                                                    width: 2*r,
                                                    height: 2*r)))
        path.append(UIBezierPath(ovalIn: bounds))
        let layer = self.layer as! CAShapeLayer
        layer.path = path.cgPath
    }
}
        maskView.bounds.size = view.bounds.size * 3
        maskView.center = imageView.bounds.center
        maskView.backgroundColor = .clear
        imageView.mask = maskView

panでcenterで行けた。


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

class ViewController: UIViewController {

    private lazy var imageView: UIImageView = {
        let v = UIImageView()
        v.isUserInteractionEnabled = true
        v.backgroundColor = .red
        v.addGestureRecognizer(pan)
        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
    }

    private var maskView = MaskView()

    override func viewDidLoad() {

        //親の青いやつ
        let original = UIView()
        original.backgroundColor = .blue
        original.frame = view.bounds.insetBy(dx: 50, dy: 150)
        view.addSubview(original)

        //子の赤
        imageView.clipsToBounds = true
        imageView.frame = original.bounds
        original.addSubview(imageView)

        //赤にマスクをかけたので、下の青いのが見える。
        maskView.bounds.size = view.bounds.size * 3
        maskView.center = imageView.bounds.center
        maskView.backgroundColor = .clear
        imageView.mask = maskView
    }
}

class MaskView: BaseView {

    override class var layerClass: AnyClass {
        return CAShapeLayer.self
    }

    override func initializeView() {

        let layer = self.layer as! CAShapeLayer
        layer.fillRule = kCAFillRuleEvenOdd
        layer.fillColor = UIColor.orange.cgColor
    }

    override func draw(_ rect: CGRect) {

        let path = UIBezierPath(rect: bounds)

        let center = bounds.center
        let r: CGFloat = 50
        path.append(UIBezierPath(ovalIn: CGRect(x: center.x-r,
                                                y: center.y-r,
                                                width: 2*r,
                                                height: 2*r)))
        path.append(UIBezierPath(ovalIn: bounds))
        let layer = self.layer as! CAShapeLayer
        layer.path = path.cgPath
    }
}
churabou commented 6 years ago

中央がgradtionで

行って以外は黒

とにかくmask用のView

class RadialGradientLayer: CAShapeLayer {

    required override init() {
        super.init()
        needsDisplayOnBoundsChange = true
        fillRule = kCAFillRuleEvenOdd
        fillColor = UIColor.black.cgColor
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    required override init(layer: Any) {
        super.init(layer: layer)
    }

    public var colors = [UIColor.clear.cgColor, UIColor.black.cgColor]

    override func draw(in ctx: CGContext) {
        ctx.saveGState()

        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let locations: [CGFloat] = [0, 1]

        let gradient = CGGradient(colorsSpace: colorSpace,
                                        colors: colors as CFArray,
                                        locations: locations)

        let center = CGPoint(x: bounds.width / 2.0, y: bounds.height / 2.0)
        let radius = min(bounds.width / 2.0, bounds.height / 2.0)
        ctx.drawRadialGradient(gradient!,
                               startCenter: center,
                               startRadius: 0.0,
                               endCenter: center,
                               endRadius: radius-250,
                               options: CGGradientDrawingOptions(rawValue: 0))

        let path = UIBezierPath(rect: bounds)
        path.append(UIBezierPath(arcCenter: center,
                                 radius: radius-250,
                                 startAngle: 0,
                                 endAngle: 2*CGFloat.pi,
                                 clockwise: true))

        self.path = path.cgPath
    }
}