Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 目标C——如何在UIView上标记点_Ios_Objective C - Fatal编程技术网

Ios 目标C——如何在UIView上标记点

Ios 目标C——如何在UIView上标记点,ios,objective-c,Ios,Objective C,我需要在视图上画一个圆。视图的位置将是一些动态发送的值, 例如,如果视图的高度为100,值为60,则应在视图内的第60个位置绘制点 这是用于显示两个视图的代码 UIView *img=[[UIView alloc]initWithFrame:CGRectMake(0,70,67,300)]; img.backgroundColor=[UIColor blackColor]; [self.view addSubview:img]; UIView *circle=[[UIView alloc]ini

我需要在视图上画一个圆。视图的位置将是一些动态发送的值, 例如,如果视图的高度为100,值为60,则应在视图内的第60个位置绘制点

这是用于显示两个视图的代码

UIView *img=[[UIView alloc]initWithFrame:CGRectMake(0,70,67,300)];
img.backgroundColor=[UIColor blackColor];
[self.view addSubview:img];
UIView *circle=[[UIView alloc]initWithFrame:CGRectMake(10,20,50,50)];
circle.alpha=0.5;
circle.layer.cornerRadius=50;
circle.backgroundColor=[UIColor whiteColor];
[img addSubview:circle];
我怎样才能做到这一点?任何人都可以帮忙

--编辑-- 试试这个

-(void)drawCircle{
UIView *img=[[UIView alloc]initWithFrame:CGRectMake(0,70,67,300)];
img.backgroundColor=[UIColor blackColor];
[self.view addSubview:img];
UIView *circle=[[UIView alloc]initWithFrame:CGRectMake(10,20,50,50)];
circle.alpha=0.5;
circle.layer.cornerRadius=circle.frame.size.width/2;
circle.backgroundColor=[UIColor whiteColor];
[img addSubview:circle];
}



note: change only the line

circle.layer.cornerRadius=circle.frame.size.width/2;
可以将UIView设置为圆形,如下所示:

circle.layer.cornerRadius = imageView.bounds.size.width/2;
circle.layer.masksToBounds = YES;
要将圆移动到新位置,可以执行以下操作:

circle.center = CGPointMake (newCenterX,newCenterY);
试试这个

- (void)drawCricleAtPoint:(CGPoint)center inView:(UIView *)superview withRadius:(CGFloat)radius
{
    CGPoint originOfSquare = CGPointMake((center.x-radius), (center.y-radius));
    CGRect squareFrame = CGRectZero;
    squareFrame.origin = originOfSquare;
    squareFrame.size = CGSizeMake(2*radius, 2*radius);
    UIView *circle = [[UIView alloc] initWithFrame:squareFrame];
    circle.layer.cornerRadius = radius; //half of the width if enclosing square
    [circle setBackgroundColor:[UIColor lightGrayColor]];
    [superview addSubview:circle];
}

您只需简单地存储动态生成的x和y的值。所以试试看。这可能对你有帮助

UIView *img=[[UIView alloc]initWithFrame:CGRectMake(0,70,67,300)];
img.backgroundColor=[UIColor blackColor];
[self.view addSubview:img];
UIView *circle=[[UIView alloc]initWithFrame:CGRectMake(x,y,50,50)];
circle.alpha=0.5;
circle.layer.cornerRadius=50;
circle.backgroundColor=[UIColor whiteColor];
[img addSubview:circle];

我的意思不是改变角半径,而是将圆移动到视图上方的某个位置,动态值float y=200;UIView*circle=[[UIView alloc]initWithFrame:CGRectMake10,y,50,50];您可以在AxisY旁边使用上面的命令来执行此操作,但是如果该值在视图上方,比如说320,该怎么办?UIView*img=[[UIView alloc]initWithFrame:CGRectMake0,70,67300];此处高度=300;所以你应该设置y,我的意思不是改变角半径,而是用动态值将圆移动到视图上方的某个位置。你可以设置圆的中心,甚至可以设置圆的边框。安排你方便的事