-1

i have a little problem. As you can see i have created a triangle (which is "context") and the triangle's angle (which is "retVinkel") using Core graphics. Now, when i run the app on Iphone 6s it looks just fine, right in the middle where i want it and the right size. Of course the triangles width, height and position will be the same because i have written the coordinates inside the UIView, for instance with the iPad air it will be in the right corner and not in the middle.

But how can i do so that the triangle stays in the middle and expand if the screen size expand so it will fill the same amount of space in every device. Can i say like "move 20% pixels to the right and 30% pixels down" so the triangle's area will be affected by the screen size.

    import UIKit

class TriangleDraw: UIView {


    override func drawRect(rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(context, 2.0)
        CGContextSetStrokeColorWithColor(context, UIColor.blackColor().CGColor)
        CGContextMoveToPoint(context, 75, 400)
        CGContextAddLineToPoint(context, 325, 400)
        CGContextAddLineToPoint(context, 325, 225)
        CGContextAddLineToPoint(context, 75, 400)

        let retVinkel = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(retVinkel, 2.0)
        CGContextSetStrokeColorWithColor(retVinkel, UIColor.blackColor().CGColor)
        CGContextMoveToPoint(retVinkel, 300, 400)
        CGContextAddLineToPoint(retVinkel, 300, 375)
        CGContextAddLineToPoint(retVinkel, 325, 375)

        CGContextStrokePath(context)
        CGContextStrokePath(retVinkel)
    }

}
A. stein
  • 87
  • 1
  • 1
  • 7

1 Answers1

1

Simply use a percentage of your bounds rect instead of fixed offsets, like

CGContextMoveToPoint(context, 0.15 * bounds.width, 0.80 * bounds.height)
CGContextAddLineToPoint(context, 0.45 * bounds.width, 0.80 * bounds.height)
...
Alex Curylo
  • 4,744
  • 1
  • 27
  • 37