我正在使用以下类绘制一个简单的描边圆:
@implementation StrokedCircle
- (id)initWithRadius:(CGFloat)radius strokeWidth:(CGFloat)strokeWidth strokeColor:(UIColor *)strokeColor
{
self = [super initWithRadius:radius];
if (self)
{
_strokeWidth = strokeWidth;
_strokeColor = strokeColor;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
NSLog(@"Drawing with color %@ and stroke width %f", self.strokeColor, self.strokeWidth);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect circleRect = CGRectInset(rect, self.strokeWidth, self.strokeWidth);
CGContextAddEllipseInRect(context, circleRect);
CGContextSetLineWidth(context, self.strokeWidth);
CGContextSetStrokeColor(context, CGColorGetComponents([self.strokeColor CGColor]));
CGContextStrokePath(context);
}
@end
Run Code Online (Sandbox Code Playgroud)
注意:超类是一个简单的圆(子类UIView),其中包含一个radius属性,并且视图的backgroundcolor设置为clearColor.
在视图控制器中,我添加以下代码viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
StrokedCircle *strokedCircle = [[StrokedCircle alloc] initWithRadius:50.0 …Run Code Online (Sandbox Code Playgroud) 我正在Swift中实现一个Circle类(子类UIView),radius它根据传入的帧在其初始化器中设置它,init(frame: CGRect)如下所示:
override init(frame: CGRect)
{
radius = frame.width/2.0
super.init(frame: frame)
}
Run Code Online (Sandbox Code Playgroud)
我还想确保从Interface Builder实例化圆圈的情况,所以我还实现了'必需的init(编码器aDecoder:NSCoder)`(我无论如何都被Xcode强制做了).
如何检索frame以某种方式包含在其中的视图的属性aDecoder.我想要实现的基本上是这样的:
required init(coder aDecoder: NSCoder)
{
var theFrame = aDecoder.someHowRetrieveTheFramePropertyOfTheView // how can I achieve this?
radius = theFrame.width/2.0
super.init(coder: aDecoder)
}
Run Code Online (Sandbox Code Playgroud) 我不明白为什么这种继承不起作用.我有以下设置:
struct Shape{}
struct Stain : Shape {}
Run Code Online (Sandbox Code Playgroud)
现在,为什么我不能做以下事情:
vector<Shape> shapes;
Stain stain();
shapes.push_back(stain);
Run Code Online (Sandbox Code Playgroud)
我希望这可以工作,因为它Stain是一个子类Shape,所以我应该能够Stain进入一个vector<Shape>?或者这在概念上是错误的,我想要做的确实不可能?
我正在使用react-navigation并且要在标题的右侧添加一个按钮,该按钮将导航到其他屏幕。但是在设置时navigationOptions,我想知道如何navigate在按下按钮时调用的回调函数中访问该函数:
const RootNavigationStack = StackNavigator({
PokemonList: {
screen: PokemonListWrapper,
navigationOptions: {
title: ({state}) => `Pokedex`,
header: ({ state, setParams }) => {
return {
visible: true,
right: (
<Button
title={'Add'}
onPress={() => // navigate to new screen here
/>
),
}
}
},
},
, {
headerMode: 'screen'
})
Run Code Online (Sandbox Code Playgroud)
有什么想法可以实现吗?我当时正在考虑使用withNavigation,但仍不清楚如何使用。
drawrect ×2
ios ×2
objective-c ×2
c++ ×1
cgcontext ×1
javascript ×1
nscoder ×1
polymorphism ×1
reactjs ×1
swift ×1