I want to get a random number to be in between my background width and to be 37 pixels away from the margins but it doesn't work
var width = UInt32(self.frame.width - 74)
var newX = Int(arc4random)%width)
var newY = Int(self.frame.height+10)
var pos = CGPoint(x: newX + 37, y: newY)
arc4random is a function, you need to call it. And you should be using arc4random_uniform anyway.
var newX = Int(arc4random_uniform(width))
Also, because Swift is still terrible about implicit conversions, you need to convert the arguments to the CGPoint:
var pos = CGPoint(x: CGFloat(newX + 37), y: CGFloat(newY))
And if you don't intend to change these later in the method, you should use let instead of var.
Related
I have a few elements that I want to lay out in a scrollView vertically with one element below the other. I have a UILabel, a UITextView, a UIImageView, another UITextView, followed by another UITextView (in the scrollView I want there to be a header, an intro paragraph, an image, a body paragraph, and a conclusion paragraph. I can't hard code the sizes of these elements because there are different dictionaries and images for different pages. How can I appropriately find the CGRect make float sizes for the frame of each view element? I am particularly having issues with the Y values.
This is the code that I have:
let startingY = (self.navigationController?.navigationBar.frame.height)! +
UIApplication.sharedApplication().statusBarFrame.size.height
//Position subViews on screen.
self.categoryTitle.frame = CGRectMake(0, startingY, self.view.bounds.width, 50)
let secondY = startingY + 50
self.categoryIntro.frame = CGRectMake(0, secondY, self.view.bounds.width, secondY + self.categoryIntro.contentSize.height)
let thirdY = secondY + self.categoryIntro.contentSize.height
self.imageView.frame = CGRectMake(0, thirdY, self.view.bounds.width, thirdY + image.size.height)
let fourthY = thirdY + image.size.height
self.categoryParagraph.frame = CGRectMake(0, fourthY, self.view.bounds.width, fourthY + self.categoryParagraph.contentSize.height)
let fifthY = fourthY + self.categoryParagraph.contentSize.height
self.categoryConclusion.frame = CGRectMake(0, fifthY, self.view.bounds.width, fifthY + self.categoryConclusion.contentSize.height)
Thanks so much!
If you want to increment the variable you're storing y in, you should use a var. The let keyword is for variables you want to keep constant.
You also don't need to add the y position to the last parameter of CGRectMake. The height will be added to the y position set in the second parameter.
You can then use the final value of the y position variable to update your scrollview's contentsize.
Additionally, if you've already positioned your scrollview below the status bar, you would start at zero.
You can then add the height of each view you add:
var yPos = 0;
self.categoryTitle.frame = CGRectMake(0, yPos, self.view.bounds.width, 50)
yPos += self.categoryTitle.frame.size.height;
self.categoryIntro.frame = CGRectMake(0, yPos, self.view.bounds.width, self.categoryIntro.contentSize.height)
yPos += self.categoryIntro.frame.size.height;
self.imageView.frame = CGRectMake(0, yPos, self.view.bounds.width, image.size.height);
yPos += self.imageView.frame.size.height;
I would like to make an effect like this, with UILabel text
how can I do that? I didn't find any help on web; I just found help with horizontal line
PS: sorry for my bad english, I'm italian :)
enoktate's answer updated to Swift 4.2, and written as an extension
extension UILabel {
/// Strikes through diagonally
/// - Parameters:
/// - offsetPercent: Improve visual appearance or flip line completely by passing a value between 0 and 1
func diagonalStrikeThrough(offsetPercent: CGFloat = 0.1) {
let linePath = UIBezierPath()
linePath.move(to: CGPoint(x: 0, y: bounds.height * (1 - offsetPercent)))
linePath.addLine(to: CGPoint(x: bounds.width, y: bounds.height * offsetPercent))
let lineLayer = CAShapeLayer()
lineLayer.path = linePath.cgPath
lineLayer.lineWidth = 2
lineLayer.strokeColor = textColor.cgColor
layer.addSublayer(lineLayer)
}
}
Looks like it is too late to answer you but this can be helpful for future googlers.
A diagonal strikethrough line can be achieved with adding a CAShapeLayer to your label as a sublayer like I did below:
let linePath = UIBezierPath()
linePath.moveToPoint(CGPointMake(0, yourLabel.bounds.height))
linePath.addLineToPoint(CGPointMake(yourLabel.bounds.width, 0))
let lineLayer = CAShapeLayer()
lineLayer.path = linePath.CGPath
lineLayer.lineWidth = 2
lineLayer.strokeColor = UIColor.lightGrayColor().CGColor
yourLabel.layer.addSublayer(lineLayer)
I would like to create a circular progress with slices where each slice is an arc.
I based my code on this answer:
Draw segments from a circle or donut
But I don't know how to copy it and rotate it 10 times.
And I would like to color it following a progress variable (in percent).
EDIT: I would like something like this
Any help please
Regards
You could use a circular path and set the strokeStart and StrokeEnd. Something like this:
let circlePath = UIBezierPath(ovalInRect: CGRect(x: 200, y: 200, width: 150, height: 150))
var segments: [CAShapeLayer] = []
let segmentAngle: CGFloat = (360 * 0.125) / 360
for var i = 0; i < 8; i++ {
let circleLayer = CAShapeLayer()
circleLayer.path = circlePath.CGPath
// start angle is number of segments * the segment angle
circleLayer.strokeStart = segmentAngle * CGFloat(i)
// end angle is the start plus one segment, minus a little to make a gap
// you'll have to play with this value to get it to look right at the size you need
let gapSize: CGFloat = 0.008
circleLayer.strokeEnd = circleLayer.strokeStart + segmentAngle - gapSize
circleLayer.lineWidth = 10
circleLayer.strokeColor = UIColor(red:0, green:0.004, blue:0.549, alpha:1).CGColor
circleLayer.fillColor = UIColor.clearColor().CGColor
// add the segment to the segments array and to the view
segments.insert(circleLayer, atIndex: i)
view.layer.addSublayer(segments[i])
}
I created this game in Spite Kit in Swift. I create a ship that I can determine its location be touching above or underneath the location of the ship. The ship will move towards the Y axis where I placed my finger. However at this point the ship will only move by a specific amount, in this case 30 or -30. How can I rewrite this code so the ship will keep on moving until my release of my finger?
Thanks!
func addShip() {
// Initializing spaceship node
ship = SKSpriteNode(imageNamed: "spaceship")
ship.setScale(0.45)
ship.zRotation = CGFloat(-M_PI/2)
// Adding SpriteKit physics body for collision detection
ship.physicsBody = SKPhysicsBody(rectangleOfSize: ship.size)
ship.physicsBody?.categoryBitMask = UInt32(shipCategory)
ship.physicsBody?.affectedByGravity = false
ship.physicsBody?.dynamic = true
ship.physicsBody?.contactTestBitMask = UInt32(obstacleCategory)
//NEWLY ADDED
ship.physicsBody?.contactTestBitMask = coinCategory
ship.physicsBody?.collisionBitMask = 0
ship.name = "ship"
ship.position = CGPointMake(120, 160)
self.addChild(ship)
actionMoveUp = SKAction.moveByX(0, y: 30, duration: 0.2)
actionMoveDown = SKAction.moveByX(0, y: -30, duration: 0.2)
}
i don't like to use SKActions for these types of things because they're really meant for one off simple animations.. However, it may be appropriate depending your game's requirements..
if you insist on using SKActions then instead of doing moveBy, why don't you just use moveTo utilizing touch location? The only thing you'd have to do is come up with a ratio based on the distance your sprite needs to travel so that your animation duration is correct. example:
let curPoint = self.sprite.position
let destinationPoint = touch.position
let diffPoint = CGPoint(
x: curPoint.x - destinationPoint.x,
y: curPoint.y - destinationPoint.y
)
let distance = sqrt(pow(diffPoint.x, 2) + pow(diffPoint.y, 2))
let durationRatio = distance / CGFloat(600)
let moveAction = SKAction.moveTo(point, duration: NSTimeInterval(durationRatio))
self.sprite.runAction(moveAction, withKey: "move")
then when you want to cancel the movement. in touchesEnded you do
self.sprite.removeActionForKey("move")
using physics velocity
let curPoint = CGPoint(x: 10, y: 10)
let destinationPoint = CGPoint(x: 100, y: 100)
let diffPoint = CGPoint(
x: curPoint.x - destinationPoint.x,
y: curPoint.y - destinationPoint.y
)
let distance = sqrt(pow(diffPoint.x, 2) + pow(diffPoint.y, 2))
let normalizedVector = CGPoint(
x: diffPoint.x / distance,
y: diffPoint.y / distance
)
let speed = CGFloat(100)
let velocity = CGVector(
dx: normalizedVector.x * speed,
dy: normalizedVector.y * speed
)
self.sprite.physicsBody!.velocity = velocity
in touchesEnded you do
self.sprite.physicsBody!.velocity = CGVector(dx: 0.0, dy: 0.0)
I'm making a top-down shooter and the player's gun is offset from the coordinates of the object. I'm using GameMaker:Studio, so the x and y coords are the center of the object. The offset of the image is set here:
bullet_offset_x = 30;
bullet_offset_y = 28;
And here is the code for shooting the gun:
var xpos = x + (bullet_offset_x * cos(degtorad(direction))) - (bullet_offset_y * sin(degtorad(direction)));
var ypos = y + (bullet_offset_x * sin(degtorad(direction))) + (bullet_offset_y * cos(degtorad(direction)));
var flash = instance_create(xpos, ypos, obj_flash);
with (flash){
direction = other.direction;
image_angle = other.direction;
}
I'm using the following formula for placing the muzzle flash:
x' = xcos(angle) - ysin(angle)
y' = xsin(angle) + ycos(angle)
Therefore:
xpos = x + x' and ypos = x + y'
However, when I run the code, the muzzle flash is correctly positioned when the angle is 0/360, but is off otherwise. Am I calculating this wrong?
IMAGES:
Correct
Incorrect
You need to use lengthdir_x and lengthdir_y functions, like:
var xpos = x + lengthdir_x(offset_distance, offset_angle + image_angle); // or direction
var ypos = y + lengthdir_y(offset_distance, offset_angle + image_angle);
var flash = instance_create(xpos, ypos, obj_flash);
flash.direction = direction;
flash.image_angle = direction;
little example here
To calculate the values to be substituted into the formula, you can use this program.
Originally it was made in Russian, but I have translated it into English. My English is terrible, but I hope you will be able to understand it.
upd: Example with offsets:
var delta_x = 60;
var delta_y = -70;
var angle = point_direction(0, 0, delta_x, delta_y);
var distance = point_distance(0, 0, delta_x, delta_y);
var xpos = x + lengthdir_x(distance, image_angle + angle);
var ypos = y + lengthdir_y(distance, image_angle + angle);
var obj = instance_create(xpos, ypos, obj_flash);
obj.image_angle = image_angle;
When your sprite has an angle of 0, your muzzle flash still at an angle of invtan(28/30) in relation to the sprite. Therefore, the angle that the flash must be placed at in relation to the rotation of the sprite can be given by
flashRotation = spriteRotationDegrees - invtan(28/30) \\you can change this to radians
Once that is found, the positions can be found by:
var x_pos = sprite_x_pos + Math.Sqrt(28^2 + 30^2)cos(flashRotation);
var y_pos = sprite_y_pos + Math.Sqrt(28^2 + 30^2)sin(flashRotation);
The actual angle of rotation of the flash (which way it points) will be the same angle as the sprite.
You may need to play with the flashRotaion equation depending upon which way is counted as a positive rotation.