我正在尝试以编程方式从标签的底部中心到图像的顶部中心画一条线,但是经过大量的搜索之后,我不知道怎么做。我有一个for循环,用于创建它们指向的图像和标签,因此需要将其写入以适合此循环,以便它创建与其标签/图像相关联的所有行,并相应地放置它们。
这是循环:
for i in 0..<hotspots.count {
let hotspotImageButton = UIButton(type: .Custom)
let hotspotTitleLabel = UILabel(frame: CGRect.zero)
let hotspotTextLabel = UILabel(frame: CGRect.zero)
hotspotImageButton.translatesAutoresizingMaskIntoConstraints = false
hotspotTitleLabel.translatesAutoresizingMaskIntoConstraints = false
hotspotTextLabel.translatesAutoresizingMaskIntoConstraints = false
let whiteHotspotImage = UIImage(named: hotspots[i].image)?.imageWithRenderingMode(.AlwaysTemplate)
hotspotImageButton.setImage(whiteHotspotImage, forState: .Normal)
hotspotImageButton.imageView?.tintColor = UIColor.redColor()
hotspotTitleLabel.text = hotspots[i].title
hotspotTextLabel.text = hotspots[i].text
hotspotTitleLabel.textColor = UIColor.whiteColor()
hotspotTitleLabel.font = UIFont(name: "Helvetica", size: 13)
hotspotTextLabel.textColor = UIColor.whiteColor()
hotspotTextLabel.font = UIFont(name: "Helvetica", size: 12)
standMapImage.addSubview(hotspotImageButton)
standMapImage.addSubview(hotspotTitleLabel)
standMapImage.addSubview(hotspotTextLabel)
hotspotImageButton.snp_makeConstraints { make in
make.leading.equalTo(standMapImage.snp_leadingMargin).multipliedBy(hotspots[i].hotspotXCoordinate)
make.top.equalTo(standMapImage.snp_topMargin).multipliedBy(hotspots[i].hotspotYCoordinate)
make.width.equalTo(standMapImage.snp_width).multipliedBy(0.1)
make.height.equalTo(hotspotImageButton.snp_width)
}
hotspotTitleLabel.snp_makeConstraints { make in
make.leading.equalTo(standMapImage.snp_leadingMargin).multipliedBy(hotspots[i].titleXCoordinate)
make.top.equalTo(standMapImage.snp_topMargin).multipliedBy(hotspots[i].titleYCoordinate)
}
hotspotTextLabel.snp_makeConstraints { make in
make.centerX.equalTo(hotspotTitleLabel.snp_centerX)
make.top.equalTo(hotspotTitleLabel.snp_bottom).inset(-4)
}
}
Picture of what I am trying to achieve
任何帮助都将是巨大的,因为我对所有这些都还很新,而google并没有为我提供太多帮助。
感谢您的任何帮助。
最佳答案
试试这个 ...!
-(void)createLineFrom:(UILabel *)base andTo:(UIImageView*)view{
CGPoint basePoint = CGPointMake(base.bounds.origin.x+base.bounds.size.width/2,base.bounds.origin.y+base.bounds.size.height/2);
CGPoint bellonPoint = CGPointMake((view.frame.origin.x+view.frame.size.width/2),view.frame.origin.y+view.frame.size.height);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:basePoint];
[path addLineToPoint:bellonPoint];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = [path CGPath];
shapeLayer.strokeColor = [UIColor redColor].CGColor;
shapeLayer.lineWidth = 2;
shapeLayer.fillColor = [[UIColor redColor] CGColor];
[self.layer addSublayer:shapeLayer];
}