亲爱的开发者们,
我正在使用xamarin(monotouch),想要绘制像Google Plus个人资料图片或其他类似的圆形图像视图...
我已经在网上搜索了一些东西,但是没有找到有用的内容。
有人可以帮助我吗?
谢谢。
亲爱的开发者们,
我正在使用xamarin(monotouch),想要绘制像Google Plus个人资料图片或其他类似的圆形图像视图...
我已经在网上搜索了一些东西,但是没有找到有用的内容。
有人可以帮助我吗?
谢谢。
对于你的需求,你可以使用 UIView
或 UIButton
。使用 UIButton
更容易处理触摸事件。
基本思路是创建一个带有特定坐标和大小的 UIButton
,并将 CornerRadius
属性设置为 UIButton
大小的一半(假设你想绘制一个圆形,宽度和高度将相同)。
你的代码可能看起来像这样(在你的 UIViewController
的 ViewDidLoad
中):
// define coordinates and size of the circular view
float x = 50;
float y = 50;
float width = 200;
float height = width;
// corner radius needs to be one half of the size of the view
float cornerRadius = width / 2;
RectangleF frame = new RectangleF(x, y, width, height);
// initialize button
UIButton circularView = new UIButton(frame);
// set corner radius
circularView.Layer.CornerRadius = cornerRadius;
// set background color, border color and width to see the circular view
circularView.BackgroundColor = UIColor.White;
circularView.Layer.CornerRadius = cornerRadius;
circularView.Layer.BorderColor = UIColor.Red.CGColor;
circularView.Layer.BorderWidth = 5;
// handle touch up inside event of the button
circularView.TouchUpInside += HandleCircularViewTouchUpInside;
// add button to view controller
this.View.Add(circularView);
最后实现事件处理程序(在UIViewController
的某个位置定义此方法):
private void HandleCircularViewTouchUpInside(object sender, EventArgs e)
{
// initialize random
Random rand = new Random(DateTime.Now.Millisecond);
// when the user 'clicks' on the circular view, randomly change the border color of the view
(sender as UIButton).Layer.BorderColor = UIColor.FromRGB(rand.Next(255), rand.Next(255), rand.Next(255)).CGColor;
}
Xamarin.iOS 是在 UI 方面对 Cocoa Touch 的封装。
https://developer.apple.com/technologies/ios/cocoa-touch.html
要画圆形,您需要使用Cocoa Touch API,也称为CoreGraphics。
http://docs.xamarin.com/videos/ios/getting-started-coregraphics/