我需要在我的動態創建的視圖中繪製形狀/線條。這是我正在嘗試的代碼,但它並沒有繪製任何東西,雖然意見被添加。在動態創建的UIView中畫線
//loc1 and loc2 are the touch locations on the view used to draw a rect
UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(loc1.x, loc1.y,loc2.x - loc1.x, loc2.y - loc1.y)];
UIGraphicsBeginImageContext(vw.bounds.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0/255.0, 0.0/255.0, 255.0/255.0, 1.0);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2);
CGContextAddRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, 50, 50));
CGContextStrokePath(UIGraphicsGetCurrentContext());
UIGraphicsEndImageContext();
[mainImageView addSubview:vw];
有你補充說,大衆在主視圖? –
你應該在UIView的-drawRect方法中做到這一點。 – Devfly
如果我在UIViewController上,我可以調用drawRect嗎? –