载:http://nachbaur.com/blog/building-a-custom-dashboard-uibutton

As part of the Parking Mobility iPhone app I’ve been building, I wanted to integrate a simple “Quick Start” help guide to show people how to use the app without being intrusive. I wanted to use the project’s branding as much as possible, and give the user a feeling like they haven’t left the main portion of the app. So I wanted the user to be comfortable with it, and always have a quick way to close the help window to get back to the app.
The end result is actually really simple. I subclassed the UIButton class, and overrode the drawRect: method to use some native drawing primitives.
//
// DNCloseButton.h
// ParkingMobility
//
// Created by Michael Nachbaur on 10-08-22.
// Copyright 2010 Decaf Ninja Software. All rights reserved.
//
#import
#import
@interface DNCloseButton : UIButton {
}
@end
//
// DNCloseButton.m
// ParkingMobility
//
// Created by Michael Nachbaur on 10-08-22.
// Copyright 2010 Decaf Ninja Software. All rights reserved.
//
#import "DNCloseButton.h"
@implementation DNCloseButton
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
CGFloat radius = self.bounds.size.width / 2;
CGFloat borderWidth = self.bounds.size.width / 10;
self.layer.backgroundColor = [[UIColor blackColor] CGColor];
self.layer.borderColor = [[UIColor whiteColor] CGColor];
self.layer.borderWidth = borderWidth;
self.layer.cornerRadius = radius;
if ([self.layer respondsToSelector:@selector(setShadowOffset:)])
self.layer.shadowOffset = CGSizeMake(0.25, 0.25);
if ([self.layer respondsToSelector:@selector(setShadowColor:)])
self.layer.shadowColor = [[UIColor blackColor] CGColor];
if ([self.layer respondsToSelector:@selector(setShadowRadius:)])
self.layer.shadowRadius = borderWidth;
if ([self.layer respondsToSelector:@selector(setShadowOpacity:)])
self.layer.shadowOpacity = 0.75;
[self setNeedsDisplay];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetShouldAntialias(ctx, true);
CGFloat xsize = self.bounds.size.width / 6;
CGFloat borderWidth = self.bounds.size.width / 10;
CGContextSaveGState(ctx);
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, borderWidth);
CGContextSetStrokeColorWithColor(ctx, [[UIColor whiteColor] CGColor]);
CGFloat width = self.bounds.size.width;
CGPoint start1 = CGPointMake(width / 2 - xsize, width / 2 - xsize);
CGPoint end1 = CGPointMake(width / 2 + xsize, width / 2 + xsize);
CGPoint start2 = CGPointMake(width / 2 + xsize, width / 2 - xsize);
CGPoint end2 = CGPointMake(width / 2 - xsize, width / 2 + xsize);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, start1.x, start1.y);
CGContextAddLineToPoint(ctx, end1.x, end1.y);
CGContextStrokePath(ctx);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, start2.x, start2.y);
CGContextAddLineToPoint(ctx, end2.x, end2.y);
CGContextStrokePath(ctx);
CGContextRestoreGState(ctx);
}
@end
Reading through the source code, you can see how I split the drawing between two different routines. First, my layer drawing which draws the border, the circle, shadows, etc is handled using raw CALayer drawing. The second block, inside drawRect:, draws the lines forming the “X” inside.
CGFloat closeSize = 26.0;
CGRect butframe = CGRectMake(0.0, 0.0, closeSize, closeSize);
butframe.origin.x = contentView.frame.origin.x - closeSize / 2;
butframe.origin.y = contentView.frame.origin.y - closeSize / 2;
DNCloseButton *button = [[[DNCloseButton alloc] initWithFrame:butframe] autorelease];
[button addTarget:self action:@selector(closePopup:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
If you find any bugs in this code, please let me know and I’ll update this post. I hope this works out as well for you as it has for me.

- 大小: 23 KB
分享到:
相关推荐
本文将深入探讨如何使用`Canvas`来在`ImageView`的左上角绘制一个三角形,并结合文字进行展示,以实现标题中提到的功能。 ### 1. `ImageView`与`Canvas`基础知识 `ImageView`是Android SDK中用于显示图像的视图组件...
例如,要在一个自定义View的中心绘制一个圆形,我们需要计算出圆心相对于View左上角的坐标。考虑到View的宽度和高度,圆心的X坐标是`(width / 2)`,Y坐标是`(height / 2)`。这是因为宽度和高度都是从0开始,所以半径...
QGraphicsScene的坐标系统与此不同,它的坐标原点也是在左上角,但它的大小可以比视口大,也可以小,甚至可以不完全包含在视口中。这就意味着,当我们在QGraphicsScene上添加图形元素时,这些元素的位置需要考虑到两...
1. **自定义View**: 首先,要实现动态绘制,我们需要创建一个自定义的`View`类,继承自`Button`或`AppCompatButton`。在这个自定义类中,我们将覆盖`onDraw()`方法,这是Android系统绘制View时调用的方法。在这里,...
在Android开发中,自定义View和自定义Button是提升应用界面独特性和功能扩展性的重要手段。自定义View允许开发者根据需求创建独特的用户界面组件,而自定义Button则是在此基础上更进一步,专注于按钮这一常见的交互...
在Android开发中,View是构建用户界面的基本元素,它的绘制流程是开发者理解并优化UI性能的关键。本篇文章将深入探讨Android中View的绘制流程,以及如何通过自定义ViewGroup进行更复杂的布局管理。 首先,我们要...
环境:VS2017+Qt5.14.2 环境匹配可以运行成功 功能: 1:实现了基本的图形:矩形、正方形、圆形、三角形、多线段、...鼠标按下后开始绘制,再次按下后停止绘制。 3:图形的拖拽、删除、撤销操作 4:曲线图形实时绘制。
在深入探讨Android View的绘制流程之前,我们先来理解一下Android UI的基本构成和绘制机制的基础。Android UI的核心组件包括Activity、Window、View以及ViewGroup,它们共同协作完成用户界面的构建与显示。其中,...
注意,由于View的默认坐标系原点在左上角,因此在调用`draw()`方法前,可能需要对Canvas做一些平移操作,以确保View的绘制位置正确。 以下是一个简单的示例代码: ```java // 创建Bitmap Bitmap bitmap = Bitmap....
在Android开发中,图片从左上角移动到右下角是一种常见的动画效果,常用于界面交互或者游戏场景中。要实现这一效果,开发者通常会利用Android的动画框架,包括属性动画(Property Animation)系统或者视图动画(View...
在自定义View中重写`onDraw()`方法,使用`Canvas`绘制圆角矩形。 5. **使用库支持** Android社区有许多第三方库可以帮助快速实现圆角按钮,例如`Material Design Components (MDC)`库中的`MaterialButton`。通过引...
在Android开发中,自定义View是一项重要的技能,它允许开发者根据需求创建独特的用户界面组件。本文将以自定义滑动按钮为例,深入解析Android自定义View的绘制过程。 首先,了解View和ViewGroup的关系至关重要。...
"Android 实现左上角...实现左上角(其他边角)倾斜的标签(环绕效果)效果需要自定义一个 View,并在 onDraw 方法中绘制标签。我们可以使用 Path 对象来绘制标签的路径,并使用 Paint 对象来设置标签的颜色和样式。
然后,我们使用Canvas的`drawLine()`方法来绘制一条从左上角到右下角的直线。当然,你可以根据需求改变线条的起点、终点、颜色、宽度甚至是线型(如虚线)。 为了在布局中显示这个自定义View,我们需要在XML布局...
本篇文章将深入探讨如何使用自定义View来绘制饼图,这在许多数据分析或者统计展示的应用场景中非常常见。 饼图是一种数据可视化工具,用于表示各部分占整体的比例。在Android中,我们可以利用Canvas类提供的绘图...
这是一个QT的demo,可以用鼠标进行矩形的绘制,根据这个demo也可以绘制出其他的多边形 QT版本:5.15.2 VS版本:2019 1、鼠标绘制一个或多个矩形 2、鼠标放在矩形上可以按住拖动 3、鼠标放在矩形边缘可以拉长或拉短...
最后,绘制阶段在`onDraw()`方法中进行,这是实现View外观的关键步骤。 在`onDraw()`方法中,主要使用Canvas对象进行绘制。Canvas提供了丰富的绘图API,如`drawRect()`, `drawText()`, `drawBitmap()`等,可以绘制...
一个自定义的左上角三角形图标,可加入文字,直接就可以使用。
参数包括矩形左上角和右下角的坐标,以及绘画样式。例如: ```java canvas.drawRect(10, 10, 100, 100, paint); ``` 通过设置`Paint`对象的样式,可以选择绘制填充矩形(`FILL`)还是空心矩形(`STROKE`)。 5. **...