致电所有专家!我见过各种帖子,说实话,我的需求与SO上的答案略有不同.
我想创建一个UI,用户可以在特定区域创建各种线条(直线,曲线,摆动等)(现在我们称之为"画布").每行可以有多个实例.然后,用户可以根据需要拖动和编辑这些行.因此,他们可以拉伸它,更改起点,终点等,甚至可以将整行拖动到画布的边界内.
我设法画线(使用drawRect)并在每一行的末尾显示可拖动的手柄(参见参考图像),用户可以在画布的边界(红色矩形)内拖动端点以满足需要.
我面临的问题是如何点击以激活特定行的编辑.因此,默认情况下,拖动句柄将不可见,并且用户可以点击该行以激活"编辑"模式,然后显示句柄(再次点按以取消选择).因此,在上图中,我希望能够检测黄色矩形中的触摸.请记住,UIView边界是整个画布区域,允许用户自由拖动,因此检测触摸显然很困难,因为还有透明区域,并且每行可以有多个实例.
这是我的代码到目前为止的行类(startHandle和endHandle是每一端的句柄):
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint startPoint = CGPointMake(self.startHandle.frame.origin.x + self.startHandle.frame.size.width/2, self.startHandle.frame.origin.y + self.startHandle.frame.size.height/2);
CGPoint endPoint = CGPointMake(self.endHandle.frame.origin.x + self.endHandle.frame.size.width/2, self.endHandle.frame.origin.y + self.endHandle.frame.size.height/2);
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self];
if (CGRectContainsPoint(CGRectMake(startPoint.x, startPoint.y, endPoint.x - startPoint.x , endPoint.y - startPoint.y), touchLocation))
{
//this is the green rectangle! I want the yellow one :)
NSLog(@"TOUCHED IN HIT AREA");
}
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, …Run Code Online (Sandbox Code Playgroud) 我做了一个光滑的NSScroller子类,但无法弄清楚如何使它覆盖在顶部NSScrollView而不是推到documentView一边.

在这里你可以看到NSCollectionView我希望100%宽的背景,并让卷轴坐在上面.目前,我必须为滚动条设置白色背景,因为使用a绘制时clearColor不显示为透明,而是显示为黑色.
我是以错误的方式来做这件事的吗?我错过了一些明显的东西吗?如何实现NSScroller位于NSScrollView内容之上的透明跟踪行为?
简单地说,我在界面构建器中有2个视图,其中一个使用界面构建器中的RGB滑块设置为颜色99,99,99.

另一个视图以编程方式着色以实现某种形状.我填写它使用:
//Obviously, this is in drawRect.
[[UIColor leadColor] set];
CGContextEOFillPath(myContext);
//And this is a category on UIColor
+ (UIColor *)leadColor {
return [UIColor colorWithWhite:99/255.0 alpha:1.0];
}
Run Code Online (Sandbox Code Playgroud)
结果:

为什么存在这种差异?
编辑:( drawRect删除了不必要的代码)
EDIT2:
所以,我在这里骗自己.."界面构建器显示RGB 99,99,99为80,80,80.我打赌它将数字偏移19." >.> ......一个绝望的人使用Xcode认为像这样的疯狂的东西..结果:

完善!!,但为什么 ???? 另一个Xcode错误?我发现过去一个月中有10个......
我正在尝试在我的iOS应用中创建下面的圈子.我知道如何制作圆圈,但我不完全确定如何获得圆弧上的点数.它必须是代码而不是图像.下面是我目前的代码.

- (void)drawRect:(CGRect)rect
{
CGPoint point;
point.x = self.bounds.origin.x + self.bounds.size.width/2;
point.y = self.bounds.origin.y + self.bounds.size.height/2;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGRect circle = CGRectMake(point.x/2,point.y-point.x/2,point.x,point.x);
CGContextAddEllipseInRect(context, circle);
CGContextStrokePath(context);
for (int i = 0; i<8; i++) {
CGRect circleMini = CGRectMake(??????,??????,point.x/4,point.x/4);
CGContextAddEllipseInRect(context, circleMini);
CGContextStrokePath(context);
}
}
Run Code Online (Sandbox Code Playgroud)
更新到答案
float cita = 0;
for (int i = 0; i<8; i++) {
CGPoint pointCir = CGPointMake(point.x/2 + radius * cos(cita) , (point.y-point.x/2) + radius * sin(cita) );
CGRect circleMini = …Run Code Online (Sandbox Code Playgroud) 我试图将一个褪色UIColor到另一个drawRect.我创建了这个函数来计算某个百分比的颜色:
- (UIColor *)colorFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor percent:(float)percent
{
float dec = percent / 100.f;
CGFloat fRed, fBlue, fGreen, fAlpha;
CGFloat tRed, tBlue, tGreen, tAlpha;
CGFloat red, green, blue, alpha;
if(CGColorGetNumberOfComponents(fromColor.CGColor) == 2) {
[fromColor getWhite:&fRed alpha:&fAlpha];
fGreen = fRed;
fBlue = fRed;
}
else {
[fromColor getRed:&fRed green:&fGreen blue:&fBlue alpha:&fAlpha];
}
if(CGColorGetNumberOfComponents(toColor.CGColor) == 2) {
[toColor getWhite:&tRed alpha:&tAlpha];
tGreen = tRed;
tBlue = tRed;
}
else {
[toColor getRed:&tRed green:&tGreen blue:&tBlue alpha:&tAlpha];
}
red = …Run Code Online (Sandbox Code Playgroud) 我有几个UIView类都绘制相同的东西,但使用不同的颜色和alpha设置.我试图传递参数,但无法弄清楚如何获得我需要的drawRect部分.
我这样画:
CGRect positionFrame = CGRectMake(widthCoordinate,heightCoordinate,20.9f,16.5f);
DrawHexBlue *hex = [[DrawHexBlue alloc] initWithFrame:positionFrame];
[self.imageView addSubview:hex];
Run Code Online (Sandbox Code Playgroud)
我的DrawHexBlue类是这样的:
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self)
{
[self setOpaque:YES];
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
Run Code Online (Sandbox Code Playgroud)
}
- (void)drawRect:(CGRect)rect{
UIBezierPath *aPath = [UIBezierPath bezierPath];
[[UIColor whiteColor] setStroke];
[[UIColor blueColor] setFill];
// Set the starting point of the shape.
[aPath moveToPoint:CGPointMake(7, 0)];
// Draw the lines.
[aPath addLineToPoint:CGPointMake(16.2, 0)];
[aPath addLineToPoint:CGPointMake(20.9, 8.7)];
[aPath addLineToPoint:CGPointMake(16.2, 16.5)];
[aPath addLineToPoint:CGPointMake(6.7, 16.5)];
[aPath addLineToPoint:CGPointMake(2.1, 8.7)];
[aPath closePath];
[aPath setLineWidth:1.0f]; …Run Code Online (Sandbox Code Playgroud) 我是我的UIView课的子类.Xcode(我使用的是4.6.3)自动生成的代码说,
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
Run Code Online (Sandbox Code Playgroud)
它在我的脑海中提出了几个问题:
1)为什么空的实现drawRect:会在动画期间导致不良性能.
2)我什么时候应该实施drawRect:.
3)如果我正在实施,drawRect:那么应采取什么措施作为最佳实践的预防措施.
我按照在线教程绘制了一个子类UIView.教程显示了一个带有白色背景的UIView,我通过简单地改变超级的bg颜色来修复它.问题是,当触摸结束时,背景不会保持清晰.我不知道.我只是尝试将填充颜色设置为[uicolor clearcolor]; 不成功.这是我正在使用的代码:
@implementation CachedLIView
{
UIBezierPath *path;
UIImage *incrementalImage; // (1)
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
{
[self setMultipleTouchEnabled:NO];
[self setBackgroundColor:[UIColor whiteColor]];
path = [UIBezierPath bezierPath];
[path setLineWidth:2.0];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
[incrementalImage drawInRect:rect]; // (3)
[path stroke];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path moveToPoint:p];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p …Run Code Online (Sandbox Code Playgroud) 我正在使用UIView的子类来绘制,这个子类视图用于在视图控制器上获取您的签名.有一个清除按钮,应该清除UIView,除非它不起作用.这是我尝试过的.
subclass.h
@implementation subclassed uiview
{
UIBezierPath *path;
UIImage *incrementalImage; // (1)
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self setMultipleTouchEnabled:NO];
[self setBackgroundColor:[UIColor clearColor]];
path = [UIBezierPath bezierPath];
[path setLineWidth:2.0];
}
return self;
}
- (void)drawRect:(CGRect)rect {
[incrementalImage drawInRect:rect]; // (3)
[path stroke];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path moveToPoint:p];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint p …Run Code Online (Sandbox Code Playgroud) 我试图在android中创建一个类似于结构的家族树.我正在使用画布为家庭成员名称和连接线绘制矩形和线条.
我在链接的帮助下通过以下方法绘制矩形和线条
DrawView.java
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;
public class DrawView extends View {
Paint paint = new Paint();
float mx, my, mdensity;
Paint mBGPaint, mTXTPaint,mLINEPaint,mBRDPaint;
String text;
public DrawView(Context context, float x, float y, float density, String text) {
super(context);
paint.setColor(Color.RED);
paint.setStrokeWidth(8);
paint.setStyle(Paint.Style.STROKE);
mx = x;
my = y;
mdensity = density;
this.text = text;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
init();
mLINEPaint.setStrokeWidth(8);
//draw rect border
canvas.drawRect(100, 100, …Run Code Online (Sandbox Code Playgroud) drawrect ×10
ios ×7
uiview ×5
objective-c ×4
cocoa-touch ×3
uicolor ×2
android ×1
cgcolor ×1
cocoa ×1
colors ×1
family-tree ×1
geometry ×1
ios8 ×1
iphone ×1
line ×1
nsscroller ×1
nsscrollview ×1
transparency ×1