斯威夫特:周围的黑色

时间:2018-07-20 11:40:17

标签: swift crop

您好,我正在创建一个裁剪照片的功能,我想在容器的外部将照片裁剪为黑色,不透明度为0.5,请看下面的图片以更好地理解:

All I drew in black is what I want it to be black with an opacity of 0.5, how to do this?

我全部用黑色绘制的就是我希望它是不透明度为0.5的黑色,怎么做?

用户可以移动并更改框架的大小,我使用此大小更改CGRect。因此有必要使黑色背景随着帧的大小和位移的变化而动态变化,我不知道该怎么做

我的UIIvire框架的类:

class ResizableView: UIView {

enum Edge {
    case topLeft, topRight, bottomLeft, bottomRight, none
}

static var edgeSize: CGFloat = 44.0
private typealias `Self` = ResizableView

var currentEdge: Edge = .none
var touchStart = CGPoint.zero

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {

        touchStart = touch.location(in: self)

        currentEdge = {
            if self.bounds.size.width - touchStart.x < Self.edgeSize && self.bounds.size.height - touchStart.y < Self.edgeSize {
                return .bottomRight
            } else if touchStart.x < Self.edgeSize && touchStart.y < Self.edgeSize {
                return .topLeft
            } else if self.bounds.size.width - touchStart.x < Self.edgeSize && touchStart.y < Self.edgeSize {
                return .topRight
            } else if touchStart.x < Self.edgeSize && self.bounds.size.height - touchStart.y < Self.edgeSize {
                return .bottomLeft
            }
            return .none
        }()
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let currentPoint = touch.location(in: self)
        let previous = touch.previousLocation(in: self)

        let originX = self.frame.origin.x
        let originY = self.frame.origin.y
        let width = self.frame.size.width
        let height = self.frame.size.height

        let deltaWidth = currentPoint.x - previous.x
        let deltaHeight = currentPoint.y - previous.y

        switch currentEdge {
        case .topLeft:
            self.frame = CGRect(x: originX + deltaWidth, y: originY + deltaHeight, width: width - deltaWidth, height: height - deltaHeight)
        case .topRight:
            self.frame = CGRect(x: originX, y: originY + deltaHeight, width: width + deltaWidth, height: height - deltaHeight)
        case .bottomRight:
            self.frame = CGRect(x: originX, y: originY, width: currentPoint.x + deltaWidth, height: currentPoint.y + deltaWidth)
        case .bottomLeft:
            self.frame = CGRect(x: originX + deltaWidth, y: originY, width: width - deltaWidth, height: height + deltaHeight)
        default:
            // Moving
            self.center = CGPoint(x: self.center.x + currentPoint.x - touchStart.x,
                y: self.center.y + currentPoint.y - touchStart.y)
        }
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    currentEdge = .none
}

// MARK: - Border

@IBInspectable public var borderColor: UIColor = UIColor.clear {
    didSet {
        layer.borderColor = borderColor.cgColor
    }
}

@IBInspectable public var borderWidth: CGFloat = 0 {
    didSet {
        layer.borderWidth = borderWidth
    }
}

}

1 个答案:

答案 0 :(得分:0)

使用maskView插入覆盖出血区域的图像。图像在出血区域应为50%不透明度。 Here's an example了解如何实施此技术。

let imageView = UIImageView(image: UIImage(named: "star"))
imageView.contentMode = .scaleAspectFill
view.maskView = imageView