Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何创建要在纵断面图屏幕中使用的对角剖视图?_Ios_Objective C_Uiview_Uiimageview - Fatal编程技术网

Ios 如何创建要在纵断面图屏幕中使用的对角剖视图?

Ios 如何创建要在纵断面图屏幕中使用的对角剖视图?,ios,objective-c,uiview,uiimageview,Ios,Objective C,Uiview,Uiimageview,我有一个屏幕,我需要在imageview之后添加斜切视图 这是我需要制作的屏幕截图 所以请帮助我创建这种视图 我已经提到 还有问题 但我想知道是否有更可靠的方法来实现这一点 提前感谢。要做到这一点,您可以将视图添加到故事板中,并将视图的类设置为自定义类,从而将视图的布局更改为对角剪切 自定义类应如下所示: // // CustomShapeView.swift // // // Created by Umar Farooque on 17/09/17. // Copyright © 20

我有一个屏幕,我需要在imageview之后添加斜切视图

这是我需要制作的屏幕截图

所以请帮助我创建这种视图

我已经提到

还有问题

但我想知道是否有更可靠的方法来实现这一点


提前感谢。

要做到这一点,您可以将视图添加到故事板中,并将视图的类设置为自定义类,从而将视图的布局更改为对角剪切

自定义类应如下所示:

//
//  CustomShapeView.swift
//
//
//  Created by Umar Farooque on 17/09/17.
//  Copyright © 2017 ufocorp. All rights reserved.
//

import UIKit
@IBDesignable
class CustomShapeView: UIView {

    @IBInspectable var color : UIColor? = UIColor.gray {
        didSet {
            //            self.layer.backgroundColor = self.color?.cgColor
        }

    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.clear
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        backgroundColor = UIColor.clear

    }

    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
        //get the size of the view
        let size = self.bounds.size
        //get 4 points for the shape layer
        let p1 = self.bounds.origin
        let p2 = CGPoint(x: p1.x + size.width, y: p1.y)
        let p3 = CGPoint(x: p2.x, y: size.height)
        let p4 = CGPoint(x: p1.x, y: size.height - 30)

        //create the path
        let path = UIBezierPath()
        path.move(to: p1)
        path.addLine(to: p2)
        path.addLine(to: p3)
        path.addLine(to: p4)
        path.close()
        (color ?? UIColor.gray).set()
        path.fill()

    }

}
结果如下:


通过一些回答,我用轻触手势(在我的例子中是一个三角形)做到了这一点:

类三角形视图


协议三角协议{
func shapeTapped()
func外型(触摸:UITouch)
}
类TriangleView:UIView{
变量颜色:CGColor=UIColor.red.CGColor
var代表:TriangleViewProtocol?
重写初始化(帧:CGRect){
super.init(frame:frame)
self.backgroundColor=UIColor.clear
}
必需的初始化?(编码器aDecoder:NSCoder){
super.init(编码者:aDecoder)
}
重写函数绘图(rect:CGRect){
//画一个有三条边的形状
self.layer.sublayers?.forEach({$0.removeFromSuperlayer()})
做{
让rectangleLayer=尝试getPathPayer(arrPathPoints:[
CGPoint(x:rect.maxX,y:rect.minY),
CGPoint(x:rect.maxX,y:rect.maxY),
CGPoint(x:rect.minX,y:rect.maxY)
])
self.layer.addSublayer(矩形层)
}抓住{
调试打印(错误)
}
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
//点击并计算视图的哪一侧被触摸
保护让触摸=触摸。首先,
让sublayers=self.layer.sublayers为?[CAShapeLayer]else{return}
让点=接触位置(in:self)
对于子层中的层{
//就我而言,我需要采取两项行动
//一个如果用户点击蓝色的一面
//和其他外部蓝色,用于将此操作发送到以下视图
如果let path=layer.path,则path.contains(点){
self.delegate?.shapeTapped()
}否则{
自我.代理?.外型(触摸:触摸)
}
}
}
func getPathPayer(arrPathPoints:[CGPoint])抛出->现金支付层{
枚举路径错误:错误{
案件需要超过2分
}
保护arrPathPoints.count>2个其他{
抛出路径错误。需要超过2点
}
让lineColor=UIColor.black
设线宽:CGFloat=2
let path=UIBezierPath()
让pathLayer=CAShapeLayer()
用于arrPathPoints.enumerated()中的(索引,路径点){
开关指数{
案例0:
路径。移动(到:路径点)
案例arrPathPoints.count-1:
path.addLine(到:路径点)
path.close()
违约:
path.addLine(到:路径点)
}
}
pathLayer.path=path.cgPath
pathLayer.strokeColor=lineColor.cgColor
pathLayer.lineWidth=线宽
pathLayer.fillColor=self.color
返回路径层
}
}
将我的控制器分类

扩展FirstView:TriangleViewProtocol{ func shapeTapped(){ //蓝面行动 } func外型(触摸:UITouch){ //将外部操作发送到下面的视图 guard let filteredView=self.view.subviews.filter({type(of:$0)!=TriangleView.self&&type(of:$0)==UIStackView.self}), let view=filteredView.hitTest(触摸位置(in:filteredView),带:nil), let button=查看为?UIButton else{return} button.sendActions(用于:.touchUpInside) } } My.xib:
现在,其他子视图(在我的例子中是stackView)在三角形在透明侧被轻触时接收轻触。

看到这个了吗。让我查一下