churabou / iOS-develop-blog

0 stars 0 forks source link

5月2日(水) viewのlayerは特別(gifあり) #18

Open churabou opened 6 years ago

churabou commented 6 years ago

Viewのlayerは特別だなと思ったのと、 1年以上経つけど、CALayerの座標を動かしてみたことがなかったので(CAAnimationで移動したことは有る)びっくりしたそのコードサンプル。

中央に大きさ150の背景redのViewと背景blueのlayerを配置してみて、(lueLayerとview.layerの座標をPanGestureで移動させてみると、 blueLayerはアニメーションした(通常のCALayerはアニメーションする)がview.layerはしなかった。(Viewのlayerは特別)

layer

class ViewController: UIViewController {

    private lazy var panGesture: 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: view)
        if sender.state == .began {
            pre = location
        }
        let dx = location.x - pre.x
        let dy = location.y - pre.y

        pre = location

//viewはlayerのdelegate        
//        redView.center.x += dx
//        redView.center.y += dy    
        redView.layer.position.x += dx
        redView.layer.position.y += dy

        blueLayer.position.x += dx
        blueLayer.position.y += dy
    }

    private var redView = UIView()
    private var blueLayer = CALayer()

    override func viewDidLoad() {

        view.addGestureRecognizer(panGesture)

        redView.backgroundColor = .red
        redView.bounds.size = .equal(to: 150)
        redView.center = view.center
        view.addSubview(redView)

        blueLayer.backgroundColor = UIColor.blue.cgColor
        blueLayer.bounds.size = .equal(to: 150)
        blueLayer.position = view.center
        view.layer.addSublayer(blueLayer)
    }
}

        CATransaction.begin()
        CATransaction.setAnimationDuration(0.0)

        blueLayer.position.x += dx
        blueLayer.position.y += dy

        CATransaction.commit()

止めるのは簡単だけど、なんでこんなことも知らなかったんだろう・・・

あと多分 layer.speed = 0もいけるらしい。

churabou commented 6 years ago
    var computedCIImage: CIImage {
        return editor.image.createCIImage()!
    }

    var storedCIImage = CIImage()
        var date = Date()
        storedCIImage = editor.image.createCIImage()!

        for i in 0..<100 {
            print(storedCIImage.extent)
        }
        print("実行時間 \(Date().timeIntervalSince(date))")

        date = Date()
        for i in 0..<100 {
            print(computedCIImage.extent)
        }

        print("実行時間 \(Date().timeIntervalSince(date))")
実行時間 0.0306060314178467
実行時間 0.351920008659363
churabou commented 6 years ago

extension UIImage {

    //縮小は確認済み、拡大はおそらく・・
    func resize(by: CGFloat) -> UIImage {

        let resize = size * by
        UIGraphicsBeginImageContextWithOptions(resize, false, scale)
        draw(in: CGRect(origin: .zero, size: resize))

        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
}
churabou commented 6 years ago

CISpotLight って使えるのかいまいちよくわからんてか

    let filter = CIFilter(name: "CISpotLight")!
    filter.setValue(inputImage, forKey: "inputImage")

    let center = CIVector(x: 350, y: 700, z: 100)

// filter.setValue(nil, forKey: "inputLightPosition") filter.setValue(center, forKey: "inputLightPointsAt")

    filter.setValue(value+2.0, forKey: "inputBrightness")

// filter.setValue(nil, forKey: "inputConcentration") // filter.setValue(CIColor.blue, forKey: "inputColor")


extension CIFilter {

    class func colorControls(image: CIImage,
                             bright: Float = 0,
                             contrast: Float = 1.0,
                             saturation: Float = 1.0) -> CIFilter {
        let filter = CIFilter.named(.colorControls)
        filter.setValue(image, forKey: "inputImage")
        filter.setValue(bright, forKey: "inputBrightness")
        filter.setValue(contrast, forKey: "inputContrast")
        filter.setValue(saturation, forKey: "inputSaturation")
        return filter
    }

    class func exposureAdjust(image: CIImage, ev: Float) -> CIFilter {
        let filter = CIFilter.named(.exposure)
        filter.setValue(image, forKey: "inputImage")
        filter.setValue(ev, forKey: "inputEV")
        return filter
    }
}

extension CIImage {

    func adjustBrightness(input: Float) -> CIImage {
        return CIFilter.colorControls(image: self, bright: input).outputImage!
    }

    func adjustContrast(input: Float) -> CIImage {
        return CIFilter.colorControls(image: self, contrast: input).outputImage!
    }

    func adjustSaturation(input: Float) -> CIImage {
        return CIFilter.colorControls(image: self, saturation: input).outputImage!
    }

    func adjustExpose(input: Float) -> CIImage {
        return CIFilter.exposureAdjust(image: self, ev: input).outputImage!
    }