我想快速创建几个圆形按钮,碰撞检测和重力作用于它们。但是每当我这样做的时候,在碰撞过程中它们就像一个正方形一样,(我尝试将图像添加到按钮或者通过代码创建它,但结果保持不变),所以任何人都可以提供帮助,因为我想创建圆形按钮每当它们碰撞或坠落时都会像圆形一样。Swift - Round Button
回答
尝试OBShapedButton
这是一个第三方的类可用于与作为图像的形状你所赐给按钮这类行为所造成的不同形状类型的按钮和按键。
https://github.com/ole/OBShapedButton
要在故事板或XIB文件中使用此只是给自定义类为OBShapedButton
到UIButton
。
或编程
obj_OBShapedButton = [OBShapedButton buttonWithType:UIButtonTypeCustom];
[obj_OBShapedButton setFrame:CGRectMake(10, 10, 160,161)];
[obj_OBShapedButton setImage:[UIImage imageNamed:@"button-normal.png"] forState:UIControlStateNormal];
[obj_OBShapedButton addTarget:self action:@selector(Buton_action) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:obj_OBShapedButton];
如果存在碰撞和重力,请改用SpriteKit。在SKNode可以工作的地方创建任何形状。
我对SpriteKit并不是很熟悉,所以我一直在避免它。我可以在ViewController中做同样的事情吗? –
@AmanKumar我只是不明白...... SpriteKit被设计为物理引擎,可处理碰撞,引力和一切。为什么不直接学习一些最简单的工具,而不是UIKit。就好像有一架免费的飞机可以把你从美国带到中国,但你却在问人们如何建造一艘木船并在太平洋上航行。 –
要真正回答原来的问题:
由于iOS版9的发布,你现在可以改变碰撞形状为圆形(或任何其他形状)。
在你UIButton
子类可以补充一点:
- (UIDynamicItemCollisionBoundsType)collisionBoundsType
{
return UIDynamicItemCollisionBoundsTypeEllipse;
}
这将设置碰撞边界为圆形,而不是默认的矩形。
- 1. 摆脱Round Rect Button(MonoTouch)的圆角
- 2. Bar button double action swift
- 3. Swift next button change textfield
- 4. Button border color - Objective C - Swift
- 5. Swift Scope Button文本颜色
- 6. PHP ROUND vs Javascript ROUND
- 7. Swift Button背景图片没有出现?
- 8. Button button =(Button)v;和Button button =(Button)findviewbyid(r.id.button1);
- 9. ROUND SQL
- 10. Round UIImageView
- 11. Swift:如何通过swift代码使用button到secondViewController
- 12. Oracle 11g,约ROUND
- 13. Round Robin Scheduler
- 14. postgresql round division
- 15. ReportViewer Round()函数
- 16. Dashdb round问题
- 17. Opencart - round()函数
- 18. BasicQos和Round Robin
- 19. NSDecimalNumber round long numbers
- 20. VBA Round函数
- 21. Golang:time.Time Round to Day
- 22. Round Specific Corners - SKShapeNode
- 23. Ruby Round Up Next
- 24. RabbitMQ Round Robin
- 25. BigDecimal setScale and round
- 26. PHP - Round number
- 27. jQuery round函数
- 28. SQL Round Number Over()
- 29. Round A Variable Autohotkey
- 30. MySQL Round 1583
谢谢你的工作......完美 –