I'm trying to update code to swift 3 but I can't find anything about CGPathAddCurveToPoint, how can I fix the error?
path is a CGMutablePath
CGPathAddCurveToPoint(path, nil, 10, 10, 20, 20, 30, 30)
error: nil is not compatible with expected argument type 'UnsafePoiter'
I found that CGPathAddLineToPoint(path, nil, 10, 10)
became path.addLine(to: p0)
Please read the CGMutablePath docs:
https://developer.apple.com/reference/coregraphics/cgmutablepath
You will find:
func addCurve(to end: CGPoint, control1: CGPoint, control2: CGPoint,
transform: CGAffineTransform = default)
Note that this is now a method, not a loose global function.
Related
I want to have a game over screen pop up, using SKSpriteNodes and it scales correctly on first try but from the second, the whole game over is small.
func spawnPlayAgainBTN() {
playAgain = SKSpriteNode(imageNamed: "buttonPlay")
playAgain.position = CGPoint(x: self.frame.midX + 54, y: self.frame.midY - 10)
playAgain.zPosition = 8
playAgain.setScale(0)
self.addChild(playAgain)
playAgain.run(SKAction.scale(to: 0.4, duration: 0.4))
}
func spawnMainMenuBTN() {
mainMenu = SKSpriteNode(imageNamed: "buttonMainMenu ")
mainMenu.position = CGPoint(x: self.frame.midX - 54, y: self.frame.midY - 10)
mainMenu.zPosition = 8
mainMenu.setScale(0)
self.addChild(mainMenu)
mainMenu.run(SKAction.scale(to: 1.0, duration: 0.4))
}
func spawnGameOverScreen() {
gameOverBG = SKSpriteNode(imageNamed: "gameOverBG")
gameOverBG.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
gameOverBG.zPosition = 7
gameOverBG.setScale(0)
self.addChild(gameOverBG)
gameOverBG.run(SKAction.scale(to: 1.0, duration: 0.4))
spawnPlayAgainBTN()
spawnMainMenuBTN()
isRunningGame = false
isShowingGameOver = true
}
Okay so I'm trying to draw a dashed line inside of a view, which should be fairly simple but no matter what I've tried I keep getting the dashes at both ends clipping and I would like to have the dash not have any clipped segments. Here's the extension I made (the purple background color is just there so I can see the frame)
extension UIView {
public func addDashedLineToView(_ color : UIColor) {
self.backgroundColor = UIColor.purple
let cgColor = color.cgColor
let shapeLayer: CAShapeLayer = CAShapeLayer()
let frameSize = self.frame.size
let shapeRect = CGRect(x: 0, y: 0, width: frameSize.width, height: frameSize.height)
shapeLayer.bounds = shapeRect
shapeLayer.position = CGPoint(x: frameSize.width / 2, y: frameSize.height)
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = cgColor
shapeLayer.lineWidth = 6
shapeLayer.lineCap = kCALineCapRound
shapeLayer.lineJoin = kCALineJoinRound
shapeLayer.lineDashPattern = [8, 10]
let path: CGMutablePath = CGMutablePath()
path.move(to: .zero)
path.addLine(to: CGPoint(x: self.frame.width, y: 0))
shapeLayer.path = path
self.layer.addSublayer(shapeLayer)
self.layer.masksToBounds = true
}
and here's a screenshot of what it ends up looking like.
not sure where I'm messing up here, but I know that I must be doing something wrong since it shouldn't be this hard to draw a dashed line in swift (I hope).
You can adjust the start position of the pattern with lineDashPhase. Try
shapeLayer.lineDashPhase = -4
I'm trying to add pageControl to my collection View which is inside my collectionView Cell, so I need to declare UIPageControl so
let CollectionViewPageControl: UIPageControl = {
let pageControl = UIPageControl()
pageControl.frame = CGRect(x: 0, y: 100, width: 10, height: 10)
pageControl.currentPage = 0
pageControl.currentPageIndicatorTintColor = UIColor.red
pageControl.pageIndicatorTintColor = UIColor.lightGray
return pageControl
}
but I got this error Cannot convert value of type '() -> _' to specified type 'UIPageControl'
so any help ?
You're declaring collectionViewPageControl as an UIPageControl, but you're assigning a closure to it.
You can do this:
let collectionViewPageControl: UIPageControl = UIPageControl()
collectionViewPageControl.frame = CGRect(x: 0, y: 100, width: 10, height: 10)
collectionViewPageControl.currentPage = 0
collectionViewPageControl.currentPageIndicatorTintColor = UIColor.red
collectionViewPageControl.pageIndicatorTintColor = UIColor.lightGray
You can read more about closures in Swift here.
And don't forget to add it to your collectionViewCell (or whatever is the parent of the UICollectionView that will be paginated).
yourCollectionViewCell.addSubview(collectionViewPageControl)
So, I have this code:
UIView.animate(withDuration: 10.0, delay: 0, options: .curveLinear , animations: {
self.gameLabel.alpha = 0
}, completion: nil)
The problem is when this code is called it instantly goes to a 0 alpha and doesn't slowly transition. What am I doing wrong?
Here is some more context for the the code:
var gameLabel = SKLabelNode()
override func didMove(to view: SKView) {
gameLabel.fontColor = UIColor.white
gameLabel.fontSize = 85
gameLabel.setScale(0.40)
gameLabel.text = "this is a game"
gameLabel.position = CGPoint(x: 0, y: 0)
gameLabel.zPosition = 0
gameLabel.alpha = 1
addChild(gameLabel)
}
func touchDown(atPoint pos : CGPoint) {
UIView.animate(withDuration: 2, animations: {
self.gameLabel.alpha = 0
})
}
So, i solved my problem by running.
self.gameLabel.run(SKAction.fadeOut(withDuration: 10))
I'm using this code piece in my Swift project.
The following code worked fine in Swift 2 but don't seem to work in Swift 3 anymore, it gives the following error:
Cannot assign value of type '(CGFloat, CGFloat, Int, Int)' to type 'CGRect'
if plusLayer == nil {
plusLayer = CALayer()
let halfWidth: CGFloat = self.bounds.size.width / 2
let halfHeight: CGFloat = self.bounds.size.height / 2
let ds: CGFloat = sqrt(halfWidth * halfWidth / 2)
let x: CGFloat = halfWidth + ds - 27 / 2
let y: CGFloat = halfHeight - ds - 27 / 2
plusLayer.frame = (x, y, 27, 27)
plusLayer.contentsGravity = kCAGravityResizeAspectFill
if let i = plusSignImage {
plusLayer.contents = i.cgImage
} else {
plusSignImage = UIImage(named: "PlusSign", in: Bundle(for: self.dynamicType), compatibleWith: UITraitCollection(displayScale: UIScreen.main().scale))
plusLayer.contents = plusSignImage!.cgImage
}
layer.addSublayer(plusLayer)
}
Anyone knows how I can solve this issue? Help'd be really appreciated!
Thanks! :D
This line plusLayer.frame = (x, y, 27, 27)
should be
plusLayer.frame = (x, y, 27 as CGFloat, 27 as CGFloat)
This is because the CGRect initializer expects 4 numbers with the same type. Take a look at the Xcode suggestions:
You need explicit casting of 27 (an Int type) to CGFloat:
plusLayer.frame = (x, y, 27 as CGFloat, 27 as CGFloat)
as an old developer, let make some clearness:
a) casting is a powerful features of some languages, but it must be known in details
b) we can cast using "as", but is NOT the same as doing CGFloat(xx)
c) "as" must be used for type casting of objects.
(see:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html
)
d) CGRectMake does work in swift 2.x/xcode 7.3
e) don't cast to double, simply use:
let r = CGRect(x: 10, y: 20, width: 30, height: 40)
compiler will understand which of these method call:
public init(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat)
public init(x: Double, y: Double, width: Double, height: Double)
public init(x: Int, y: Int, width: Int, height: Int)
and will call the 3rd.
If You write:
let d = 10.0
let r2 = CGRect(x: d, y: 20, width: 30, height: 40)
it will call 2nd, as d is a Double.
my two cents.