首页 > 解决方案 > Swift:视野黑色

问题描述

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

我用黑色画的只是我希望它是黑色,不透明度为 0.5,如何做到这一点?

我用黑色画的只是我希望它是黑色,不透明度为 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
    }
}

}

标签: swiftcrop

解决方案


使用maskView插入覆盖出血区域的图像。图像在出血区域的不透明度应为 50%。这是一个有关如何实现此技术的示例。

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

推荐阅读