Iphone 如何将UIButtons添加到自定义视图(cutom drawRect)
此iOS5+项目正在使用故事板,ARC处于启用状态 我正在制作一个内部带有“圆圈”的自定义视图(它将重新显示一次点击,这只是一个背景图像),我需要在其中绘制代表时间跨度的圆圈段(我已经在drawRect中这样做了)(代码中会更清楚) 现在,timespans表示一个事件,它来自事件数组,包含事件项(代码中也更清晰) 现在我要做的是在圆圈部分的顶部绘制按钮,这些按钮需要链接到特定的事件。(这可以很容易地将标识符添加到与ID对应的按钮中,并在单击时打开显示标题和按钮的注释。 因此,基本上我希望这些按钮的行为类似于mapkit中的地图注释,然后能够单击按钮并进入详细信息页面(我已经制作了该页面,并且我正在使用该页面创建mapView) (我还想为这些按钮使用自定义图像) 我将如何处理这个问题 此任务的重要文件的文件结构为:Iphone 如何将UIButtons添加到自定义视图(cutom drawRect),iphone,objective-c,ios5,drawrect,Iphone,Objective C,Ios5,Drawrect,此iOS5+项目正在使用故事板,ARC处于启用状态 我正在制作一个内部带有“圆圈”的自定义视图(它将重新显示一次点击,这只是一个背景图像),我需要在其中绘制代表时间跨度的圆圈段(我已经在drawRect中这样做了)(代码中会更清楚) 现在,timespans表示一个事件,它来自事件数组,包含事件项(代码中也更清晰) 现在我要做的是在圆圈部分的顶部绘制按钮,这些按钮需要链接到特定的事件。(这可以很容易地将标识符添加到与ID对应的按钮中,并在单击时打开显示标题和按钮的注释。 因此,基本上我希望这些按
Event.h
Event.m
RadarViewController.h
RadarViewController.m
RadarView.h
RadarView.m
时钟的绘制发生在RadarView.m的drawRect中
在故事板中有一个UIView和一个RadarViewController类,它上面有一个子类和一个RadarView类。是RadarView的drawRect绘制时钟(在那里实现了drawRect函数)
我可以通过单例访问事件数组(在代码中会很清楚)
-----编码-----
这是我当前的drawRect函数:
RadarView.m
- (void) drawRect:(CGRect)dirtyRect{
CGRect bounds = [self bounds];
CGPoint center;
center.x = (bounds.origin.x + bounds.size.width)/2.0;
center.y = center.x;
// Set context being drawn upon
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor blackColor] setStroke];
CGContextAddArc(context, center.x, center.y, center.x, 0, 2*M_PI, YES);
CGContextStrokePath(context);
SingletonManager *sharedManager = [SingletonManager sharedManager];
Event *tmp;
double radius;
double startRad;
double endRad;
if(currentLocation == nil){
currentLocation = [sharedManager currentLocation].location;
}
for(int i = 0; i < [sharedManager.eventsManager count]; i++){
tmp = [sharedManager.eventsManager objectAtIndex:i];
CLLocation *loc = [[CLLocation alloc] initWithLatitude:tmp.coordinate.latitude longitude:tmp.coordinate.longitude];
CLLocationDistance distance = [currentLocation distanceFromLocation:loc];
if(distance>0){
while(distance > 400.0){
distance -= 400.0;
}
radius= distance / 400.0;
radius *= center.y;
[[UIColor redColor] setStroke];
CGContextSetLineWidth(context, 5);
CGContextSetAlpha(context, 0.6);
startRad = [self getRadians:tmp.start];
endRad = [self getRadians:tmp.einde];
CGContextAddArc(context, center.x, center.y, radius, startRad, endRad, YES);
CGContextStrokePath(context);
}
}
}
RadarView.m
-(void)drawRect:(CGRect)dirtyRect{
CGRect边界=[自边界];
点中心;
center.x=(bounds.origin.x+bounds.size.width)/2.0;
中心y=中心x;
//设置正在使用的上下文
CGContextRef context=UIGraphicsGetCurrentContext();
[[UIColor blackColor]设定行程];
CGContextAddArc(上下文,center.x,center.y,center.x,0,2*M_PI,YES);
CGContextStrokePath(上下文);
SingletonManager*sharedManager=[SingletonManager sharedManager];
事件*tmp;
双半径;
双星;
双端无线电;
如果(currentLocation==nil){
currentLocation=[sharedManager currentLocation]。位置;
}
对于(int i=0;i<[sharedManager.eventsManager计数];i++){
tmp=[sharedManager.eventsManager对象索引:i];
CLLocation*loc=[[CLLocation alloc]initWithLatitude:tmp.coordinate.latitude-longitude:tmp.coordinate.longitude];
CLLocationDistance距离=[currentLocation distanceFromLocation:loc];
如果(距离>0){
同时(距离>400.0){
距离-=400.0;
}
半径=距离/400.0;
半径*=中心y;
[[UIColor redColor]设置行程];
CGContextSetLineWidth(上下文,5);
CGContextSetAlpha(上下文,0.6);
startRad=[self-getRadians:tmp.start];
endRad=[self-getRadians:tmp.einde];
CGContextAddArc(上下文、中心.x、中心.y、半径、startRad、endRad、YES);
CGContextStrokePath(上下文);
}
}
}
--更新--
我想在RadarView中尝试的是:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(@"This is called <3");
UIButton *btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 25, 25);
btn.backgroundColor = [UIColor clearColor];
[btn setTitle:@"test" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
return self;
}
-(id)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
如果(自我){
NSLog(@"这被称为只是将按钮添加为子视图?还是我没有抓住要点?@jrturton这些是动态创建的?所以不在故事板中?我只是对“编码”视图不熟悉,你能告诉我我该怎么做吗?给它们一个“标识符”以及如何给它们添加一个“注释”,等等:你其余的评论太宽泛了,无法回答她e、 哈哈…UIView参考。你认为我没有检查吗?你认为我是如何得到圆圈部分的要点的--你在drawRect方法中做得太多了。