J'ai besoin de dessiner une ligne horizontale dans un UIView. Quelle est la manière la plus simple de le faire. Par exemple, je veux dessiner une ligne horizontale noire à y-coord = 200.
Je n'utilise PAS Interface Builder.
J'ai besoin de dessiner une ligne horizontale dans un UIView. Quelle est la manière la plus simple de le faire. Par exemple, je veux dessiner une ligne horizontale noire à y-coord = 200.
Je n'utilise PAS Interface Builder.
Réponses:
Le moyen le plus simple dans votre cas (ligne horizontale) est d'ajouter une sous-vue avec une couleur de fond et un cadre noirs [0, 200, 320, 1]
.
Exemple de code (j'espère qu'il n'y a pas d'erreur - je l'ai écrit sans Xcode):
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 1)];
lineView.backgroundColor = [UIColor blackColor];
[self.view addSubview:lineView];
[lineView release];
// You might also keep a reference to this view
// if you are about to change its coordinates.
// Just create a member and a property for this...
Une autre façon consiste à créer une classe qui dessinera une ligne dans sa méthode drawRect (vous pouvez voir mon exemple de code pour cela ici ).
C'est peut-être un peu tard, mais je tiens à ajouter qu'il existe une meilleure solution. Utiliser UIView est simple, mais relativement lent. Cette méthode remplace la façon dont la vue se dessine elle-même et est plus rapide:
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(context, 2.0f);
CGContextMoveToPoint(context, 0.0f, 0.0f); //start at this point
CGContextAddLineToPoint(context, 20.0f, 20.0f); //draw to this point
// and now draw the Path!
CGContextStrokePath(context);
}
CGContextBeginPath(context);
avant CGContextMoveToPoint(...);
?
Voici comment vous pouvez dessiner une ligne grise à la fin de votre vue (même idée que la réponse de b123400)
class CustomView: UIView {
override func draw(_ rect: CGRect) {
super.draw(rect)
if let context = UIGraphicsGetCurrentContext() {
context.setStrokeColor(UIColor.gray.cgColor)
context.setLineWidth(1)
context.move(to: CGPoint(x: 0, y: bounds.height))
context.addLine(to: CGPoint(x: bounds.width, y: bounds.height))
context.strokePath()
}
}
}
Vous pouvez utiliser la classe UIBezierPath pour cela:
Et pouvez dessiner autant de lignes que vous le souhaitez:
J'ai sous-classé UIView:
@interface MyLineDrawingView()
{
NSMutableArray *pathArray;
NSMutableDictionary *dict_path;
CGPoint startPoint, endPoint;
}
@property (nonatomic,retain) UIBezierPath *myPath;
@end
Et initialisé les objets pathArray et dictPAth qui seront utilisés pour le dessin au trait. J'écris la partie principale du code de mon propre projet:
- (void)drawRect:(CGRect)rect
{
for(NSDictionary *_pathDict in pathArray)
{
[((UIColor *)[_pathDict valueForKey:@"color"]) setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
[[_pathDict valueForKey:@"path"] strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
[[dict_path objectForKey:@"color"] setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
[[dict_path objectForKey:@"path"] strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
méthode touchesBegin:
UITouch *touch = [touches anyObject];
startPoint = [touch locationInView:self];
myPath=[[UIBezierPath alloc]init];
myPath.lineWidth = currentSliderValue*2;
dict_path = [[NSMutableDictionary alloc] init];
touchesMoved, méthode:
UITouch *touch = [touches anyObject];
endPoint = [touch locationInView:self];
[myPath removeAllPoints];
[dict_path removeAllObjects];// remove prev object in dict (this dict is used for current drawing, All past drawings are managed by pathArry)
// actual drawing
[myPath moveToPoint:startPoint];
[myPath addLineToPoint:endPoint];
[dict_path setValue:myPath forKey:@"path"];
[dict_path setValue:strokeColor forKey:@"color"];
// NSDictionary *tempDict = [NSDictionary dictionaryWithDictionary:dict_path];
// [pathArray addObject:tempDict];
// [dict_path removeAllObjects];
[self setNeedsDisplay];
touchesEnded, méthode:
NSDictionary *tempDict = [NSDictionary dictionaryWithDictionary:dict_path];
[pathArray addObject:tempDict];
[dict_path removeAllObjects];
[self setNeedsDisplay];
Basé sur la réponse de Guy Daher.
J'essaye d'éviter d'utiliser? car cela peut provoquer un plantage de l'application si GetCurrentContext () renvoie nil.
Je ne vérifierais rien si la déclaration:
class CustomView: UIView
{
override func draw(_ rect: CGRect)
{
super.draw(rect)
if let context = UIGraphicsGetCurrentContext()
{
context.setStrokeColor(UIColor.gray.cgColor)
context.setLineWidth(1)
context.move(to: CGPoint(x: 0, y: bounds.height))
context.addLine(to: CGPoint(x: bounds.width, y: bounds.height))
context.strokePath()
}
}
}
if
clause est simplement de déballer de l'option, préférez utiliser une guard
instruction à la place.