我试图以编程方式从标签的底部中心到图像的顶部中心画一条线,但经过大量的谷歌搜索,我无法弄清楚如何。我有一个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
任何帮助都会很棒,因为我对所有这些都很新,谷歌并没有为我提供太多帮助。
感谢您提前提供任何帮助。
答案 0 :(得分:2)
试试这个......!
-(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];
}
答案 1 :(得分:2)
以下是swift
语言中的@ srinivas n代码
func createLineFrom(base: UILabel, andTo view: UIImageView) {
var basePoint = CGPointMake(base.bounds.origin.x + base.bounds.size.width / 2, base.bounds.origin.y + base.bounds.size.height / 2)
var bellonPoint = CGPointMake((view.frame.origin.x + view.frame.size.width / 2), view.frame.origin.y + view.frame.size.height)
var path = UIBezierPath()
path!.moveToPoint(basePoint)
path!.addLineToPoint(bellonPoint)
var shapeLayer = CAShapeLayer.layer
shapeLayer.path = path!.CGPath()
shapeLayer.strokeColor = UIColor.redColor().CGColor
shapeLayer.lineWidth = 2
shapeLayer.fillColor = UIColor.redColor().CGColor
self.layer.addSublayer(shapeLayer!)
}
试试吧