0

只是我是在 ipad 上开发的初学者,当我单击或触摸按钮时,我需要在 x,y 点绘制具有宽度和高度的矩形。

我在谷歌上搜索,但我没有在按钮处理程序中找到任何工作

4

2 回答 2

1

-(void)rectangleButtonSelected在 viewDidLoad 方法中创建 rectangleButton 并在 ViewController.m 中编写 方法。并创建RectangleViewUIView 类。

-(void)rectangleButtonSelected{

    RectangleView *temp = [[RectangleView alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];

    temp.center = CGPointMake(arc4random()%100, arc4random()%200);
    NSLog(@"The Main center Point : %f  %f",temp.center.x,temp.center.y);

    [self.view addSubview:temp];

}

- (void)drawRect:(CGRect)rect在 RectanglerView.m 中实现方法

- (void)drawRect:(CGRect)rect
{

    // Drawing code

    context =UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    // And draw with a blue fill color
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
    // Draw them with a 2.0 stroke width so they are a bit more visible.
    CGContextSetLineWidth(context, 2.0);


    CGContextAddRect(context, self.bounds);
    CGContextStrokePath(context);


    // Close the path
    CGContextClosePath(context);
    // Fill & stroke the path
    CGContextDrawPath(context, kCGPathFillStroke);
}

希望对你有帮助

于 2012-05-10T05:15:02.017 回答
0

您实际上需要对状态执行此操作。只有一个地方可以让你画画,那就是drawRect:. 因此,当用户单击按钮时,您需要设置一些实例变量,例如_isShowingRectangle或其他东西,然后调用setNeedsDisplay您正在绘制的自定义 UIView。

然后,在您的自定义视图中drawRect:,您可以检查是否设置了该状态变量,并绘制(或不绘制)矩形。绘图代码取决于您使用的图形层,但它可能类似于

CGContextRef ctxt = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctxt,[[UIColor blackColor] CGColor]);
CGContextSetLineWidth(ctxt,3.0);
CGContextStrokeRect(ctxt,CGRectMake(10,20,100,50));
于 2012-05-10T05:09:18.613 回答