IOS UIDynamicAnimator masksToBounds 不会将碰撞作为边界
IOS UIDynamicAnimator masksToBounds does not make collision work as bounds
我在 Xcode 7 工作,申请 IOS 9。目标是使用 UIDynamicAnimator、UIGravityBehavior 和 UICollisionBehavior 让圆形物体表现得像球一样。
一切正常,除了 UIViews(代表球)看起来是圆形的,但在碰撞过程中表现得像矩形。
构建和添加 'balls' 的代码是:
-(void) addBall{
CGRect frame;
frame.origin=CGPointZero;
frame.size = [self randomsize];
int x = (arc4random()%(int)self.inputView.bounds.size.width);
frame.origin.x = x;
UIView *bullet = [[UIView alloc] initWithFrame:frame];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10,40,20)];
label.text = @"label";
bullet.backgroundColor = [self randomColor];
bullet.layer.cornerRadius = bullet.frame.size.width/2;
bullet.layer.borderWidth = 2.0;
bullet.layer.borderColor = [UIColor blackColor].CGColor;
bullet.layer.masksToBounds=YES;
[bullet addSubview:label];
[self.inputView addSubview:bullet];
[self.gravity addItem:bullet];
[self.collider addItem:bullet];
}
需要设置哪个 属性 才能让这些对象表现得像圆形?
下面是应用程序的屏幕,显示重力和碰撞如何影响 ground/border 上的形状。
我在屏幕截图上绘制了矩形边框以显示不可见的真实边框,但防止球表现为圆形对象。
您应该继承 UIView 并重载 collisionBoundsType。类似于:
-(UIDynamicItemCollisionBoundsType) collisionBoundsType
{
return UIDynamicItemCollisionBoundsTypeEllipse;
}
在 Swift 中你可以只做一个扩展而不是创建一个子 class 这可能比 beyowulfs 的答案更容易,可以把它放在你的 class 的底部如果你只需要它一次
extension UIView {
func collisionBoundsType () -> (UIDynamicItemCollisionBoundsType) {
return UIDynamicItemCollisionBoundsType.ellipse
}
}
我在 Xcode 7 工作,申请 IOS 9。目标是使用 UIDynamicAnimator、UIGravityBehavior 和 UICollisionBehavior 让圆形物体表现得像球一样。 一切正常,除了 UIViews(代表球)看起来是圆形的,但在碰撞过程中表现得像矩形。 构建和添加 'balls' 的代码是:
-(void) addBall{
CGRect frame;
frame.origin=CGPointZero;
frame.size = [self randomsize];
int x = (arc4random()%(int)self.inputView.bounds.size.width);
frame.origin.x = x;
UIView *bullet = [[UIView alloc] initWithFrame:frame];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10,40,20)];
label.text = @"label";
bullet.backgroundColor = [self randomColor];
bullet.layer.cornerRadius = bullet.frame.size.width/2;
bullet.layer.borderWidth = 2.0;
bullet.layer.borderColor = [UIColor blackColor].CGColor;
bullet.layer.masksToBounds=YES;
[bullet addSubview:label];
[self.inputView addSubview:bullet];
[self.gravity addItem:bullet];
[self.collider addItem:bullet];
}
需要设置哪个 属性 才能让这些对象表现得像圆形?
下面是应用程序的屏幕,显示重力和碰撞如何影响 ground/border 上的形状。 我在屏幕截图上绘制了矩形边框以显示不可见的真实边框,但防止球表现为圆形对象。
您应该继承 UIView 并重载 collisionBoundsType。类似于:
-(UIDynamicItemCollisionBoundsType) collisionBoundsType
{
return UIDynamicItemCollisionBoundsTypeEllipse;
}
在 Swift 中你可以只做一个扩展而不是创建一个子 class 这可能比 beyowulfs 的答案更容易,可以把它放在你的 class 的底部如果你只需要它一次
extension UIView {
func collisionBoundsType () -> (UIDynamicItemCollisionBoundsType) {
return UIDynamicItemCollisionBoundsType.ellipse
}
}