在按钮上绘制矩形在目标c IOS5中的viewcontroller类中单击 [英] drawing rectangle on button click in viewcontroller class in objective c IOS5

查看:77
本文介绍了在按钮上绘制矩形在目标c IOS5中的viewcontroller类中单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

simply I am beginner in developing on ipad and I need to draw rectangle at point x,y with width and height when i clicked or touched button .

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

I searched on google but i didn't find anything working in button handler

推荐答案

在viewDidLoad方法中创建rectangleButton并写入 - (void)rectangleButtonSelected ViewController.m中的方法。并且还创建UIView的类 RectangleView

Create rectangleButton in viewDidLoad method and write -(void)rectangleButtonSelected method in your ViewController.m. And also create class RectangleView of UIView.

-(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中的方法

Implement - (void)drawRect:(CGRect)rect method in 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);
}

我希望它会对你有所帮助

I hope it will be helpful to you

这篇关于在按钮上绘制矩形在目标c IOS5中的viewcontroller类中单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆