绘制多边形和不规则按钮

绘制多边形和不规则按钮。要结合上次提到的贝塞尔曲线和CAShapeLayer,去绘制你想要的形状。然后通过layer层的遮罩mask,进行设置,但仅仅这样是不够的,没有遮挡的部分也会响应点击。所以要重写Button的这个方法:pointInside,之后会详细介绍


先简单介绍一下遮罩mask这个属性:
比如layerA是layerB的mask,即layerB.mask = layerA; 那么layerA上透明的部分,会被绘制成白色挡住layerB(貌似都是白色,不知道能不能弄成其他颜色); layerA上不透明的部分,会被绘制成透明,显示出layerB的内容
即把你用贝塞尔曲线绘制的shapeLayer赋给button的layer.mask就OK了。


然后介绍一下 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event:点击button时,会回调这个方法,参数是你点击的坐标。在里面判断该坐标点是否在path内,YES则响应,NO则不响应。



自定义封装的Button:

//绘制按钮时添加path遮罩

- (void)drawRect:(CGRect)rect

{

    [super drawRect:rect];

    CAShapeLayer *shapLayer = [CAShapeLayer layer];

    shapLayer.path = self.path.CGPath;

    self.layer.mask = shapLayer;

}


//覆盖方法,点击时判断点是否在path内,YES则响应,NO则不响应

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event

{

    BOOL res = [super pointInside:point withEvent:event];

    if (res)

    {

        if ([self.path containsPoint:point])

        {

            return YES;

        }

        return NO;

    }

    return NO;

}





ViewController:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Using CVtxButton Implementing the CVtxButton class is surprisingly easy: Include Vtx.h, Vtx.ccp, VtxButton.h, and VtxButton.cpp in your project. Drop a button on your dialog in Developer Studio. Add #include "VtxButton.h" immediately before #include "MyDlg.h" in MyDlg.cpp and MyApp.cpp. For each button on the dialog that you want to be a CVtxButton, add CVtxButton m_cButton1; immediately after //{{AFX_DATA(CMyDlg) in the public section of your dialog class' specification. Also add DDX_Control(pDX, IDC_BUTTON1, m_cButton1); immediately after //{{AFX_DATA_MAP(CMyDlg) in DoDataExchange() for each button. This is enough to create a default CVtxButton. The button is drawn as a rectangle that takes up the entire client area. The sides will not be shaded the same as CButton because they are colored according to the difference of their angle and the angle of light source. If you use at least one CVtxButton on a dialog, it is suggested that you change all your CButton's to CVtxButton's to maintain a coordinated look. If you want to change the look of the CVtxBuUtton, there are two ways of doing so: Use a predefined shape by adding m_cButton1.SetVtx(VTX_RECT); to OnInitDialog() in MyDlg.cpp. There are four predefined shapes which can be passed as an argument to SetVtx(): VTX_RECT VTX_DIAMOND VTX_CIRCLE VTX_STRETCHEDCIRCLE Create a CVtxPolygons object and pass it as an argument to SetVtx(): CRect rect; m_cButton1.GetClientRect(&rect); // Get the button's original dimensions CVtxPolygons vtxPolygons; int offset[4] = {0, 1, 2, 4}; for (int i = 0; i < 4; i++) // Iterate through each of the polygons { // Add the corners vtxPolygons.Add(i, CVertex(rect.left + offset[i], rect.top + offset[i])); vtxPolygons.Add(i, CVertex(rect.right - offset[i] - 1, rect.top + offset[i])); vtxPolygons.Add(i, CVertex(rect.right - offset[i] - 1, rect.bottom - offset[i] - 1)); vtxPolygons.Add(i, CVertex(rect.left + offset[i], rect.bottom - offset[i]
在Qt中,创建一个不规则的组合按钮,即在按钮中央有一个不规则的形状,并且左右两侧有箭头按钮的组合,可以按照以下步骤进行: 1. 首先,在Qt中创建一个按钮控件,并将其设置为透明背景。在Qt Creator的设计视图中,将其大小调整为所需的大小,并将背景颜色设置为透明。 2. 借助Qt的图形模块,可以自定义绘制按钮的外观。在按钮绘制事件中,重写按钮类的`paintEvent()`函数。在该函数中,可以使用绘图API来绘制按钮的形状和箭头。 3. 绘制不规则的形状:可以使用Qt的`QPainter`类来绘制按钮的形状,例如绘制一个多边形或自定义的形状,可以使用`drawPolygon()`函数或`drawPath()`函数来实现。 4. 绘制箭头按钮:可以在按钮的左右两侧使用额外的按钮控件来表示箭头。通过创建两个按钮,设置其背景图片为箭头图标,并将其放置在不规则形状的两侧,形成左右箭头的按钮组合。 5. 执行按钮的功能和交互:可以为按钮组合绑定对应的功能和信号槽,例如点击箭头按钮后的交互行为,可以通过连接按钮的`clicked()`信号与对应的槽函数来实现。 6. 最后,在程序中使用该自定义的不规则组合按钮。将其添加到相应的窗口或布局中,并为其设置位置和大小,以便在界面中显示和交互。 通过以上步骤,我们可以在Qt中创建一个自定义的不规则组合按钮,该按钮包含了一个不规则的形状,并在左右两侧添加了箭头按钮,实现了独特的外观和交互功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值