Chỉ cần nghĩ rằng tôi muốn thêm rằng bạn không nhất thiết phải vẽ điều này trong phương thức "drawRect:" của UIView. Bạn có thể vẽ nó bất cứ nơi nào bạn muốn cung cấp bạn làm điều đó bên trong một bối cảnh hình ảnh UIGraphics. Tôi làm điều này tất cả thời gian khi tôi không muốn tạo một lớp con của UIView. Dưới đây là ví dụ hoạt động:
UIBezierPath *circle = [UIBezierPath
bezierPathWithOvalInRect:CGRectMake(75, 100, 200, 200)];
//you have to account for the x and y values of your UIBezierPath rect
//add the x to the width (75 + 200)
//add the y to the height (100 + 200)
UIGraphicsBeginImageContext(CGSizeMake(275, 300));
//this gets the graphic context
CGContextRef context = UIGraphicsGetCurrentContext();
//you can stroke and/or fill
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
[circle fill];
[circle stroke];
//now get the image from the context
UIImage *bezierImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *bezierImageView = [[UIImageView alloc]initWithImage:bezierImage];
Bây giờ, chỉ cần thêm UIImageView làm chế độ xem phụ.
Ngoài ra, bạn cũng có thể sử dụng tính năng này cho bản vẽ khác. Một lần nữa, sau một chút thiết lập, nó hoạt động giống như phương thức drawRect:
//this is an arbitrary size for example
CGSize aSize = CGSizeMake(50.f, 50.f);
//this can take any CGSize
//it works like the frame.size would in the drawRect: method
//in the way that it represents the context's size
UIGraphicsBeginImageContext(aSize);
//this gets the graphic context
CGContextRef context = UIGraphicsGetCurrentContext();
//you can do drawing just like you would in the drawRect: method
//I am drawing a square just for an example to show you that you can do any sort of drawing in here
CGContextMoveToPoint(context, 0.f, 0.f);
CGContextAddLineToPoint(context, aSize.width, 0.f);
CGContextAddLineToPoint(context, aSize.width, aSize.height);
CGContextAddLineToPoint(context, 0.f, aSize.height);
CGContextClosePath(context);
//you can stroke and/or fill
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
CGContextDrawPath(context, kCGPathFillStroke);
//now get the image from the context
UIImage *squareImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *squareImageView = [[UIImageView alloc]initWithImage:squareImage];
Edit: Một điều tôi nên thêm là đối với bất kỳ bản vẽ hiện đại ngày nay của loại hình này, bạn nên trao đổi ra
UIGraphicsBeginImageContext(size);
cho
UIGraphicsBeginImageContextWithOptions(size, opaque, scale);
này sẽ vẽ đồ họa của bạn một cách chính xác cho màn hình võng mạc và không võng mạc.
FYI, UIGraphicsBeginImageContext(size)
tương đương với UIGraphicsBeginImageContextWithOptions(size, FALSE, 1.f)
, không có màn hình võng mạc nào có thể có độ trong suốt.
Tuy nhiên, nếu bạn không cần độ trong suốt, nó được tối ưu hóa hơn để chuyển TRUE cho đối số mờ.
Cách vẽ an toàn nhất và được đề xuất là để vượt qua trong [[UIScreen mainScreen]scale]
làm đối số quy mô.
Vì vậy, cho ví dụ (s) ở trên, bạn sẽ sử dụng này để thay thế:
UIGraphicsBeginImageContextWithOptions(aSize, FALSE, [[UIScreen mainScreen] scale]);
Mọi chi tiết, kiểm tra Apple's docs.
Tôi cố gắng này, nhưng không có gì xảy ra. Làm thế nào để tôi có được bản vẽ xảy ra trong chế độ xem tùy chỉnh của tôi? – joec
Tạo lớp con UIView và thực hiện phương thức drawRect ở đó, nó hoạt động với tôi ít nhất – Vladimir
Đây là cũ, nhưng trong trường hợp nó giúp người khác: Tôi quên xác định rằng giao diện của tôi trong Interface Builder là loại "MyView" (nhận dạng lớp) – Mirkules