2

Estou tentando de várias formas e não estou obtendo sucesso, consegui de um jeito que não ficará viável, alguém sabe como eu poderia fazer mais o menos nessa ideia pegar a posição central do circulo e calcular pontos(x, y) q estejam externos ao raio.

func pressedAddBubble(sender : UIButton!){
    let direction = self.mainBubble!.getDirection()

    let width = CGFloat(self.mainBubble!.getWidth() * 0.8)
    let height = CGFloat(self.mainBubble!.getHeight() * 0.8)

    if direction == 0 {
        //print("leste")
        //adiciona as bolha ao Leste da bolha principal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.minX + self.mainBubble!.getWidth() + self.mainBubble!.getWidth() * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.minY + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()

    } else if direction == 1 {
        //print("nordeste")
        //adiciona a 1 bolha ao Nordeste da bolha pricipal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.maxX + self.mainBubble!.getWidth() * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.minY - self.mainBubble!.getHeight() + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()

    } else if direction == 2 {
        //print("norte")
        //adiciona as bolhas ao Norte da bolha principal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.minX + self.mainBubble!.getWidth() * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.minY - self.mainBubble!.getHeight() + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()

    } else if direction == 3 {
        //print("noroeste")
        //adiciona as bolhas ao Noroeste da bolha principal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.minX - self.mainBubble!.getWidth() + (self.mainBubble!.getWidth()) * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.minY - self.mainBubble!.getHeight() + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()
    } else if direction == 4 {
        //print("oeste")
        //adiciona as bolhas ao Oeste da bolha principal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.minX - self.mainBubble!.getWidth() + self.mainBubble!.getWidth() * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.minY + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()
    } else if direction == 5 {
        //print("sudoeste")
        //adiciona as bolhas ao Sudoeste da bolha principal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.minX - self.mainBubble!.getWidth() + (self.mainBubble!.getWidth()) * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.maxY + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()
    } else if direction == 6 {
        //print("sul")
        //adiciona as bolhas ao Sul da bolha principal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.minX + (self.mainBubble!.getWidth()) * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.maxY + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()
    } else if direction == 7 {
        //print("sudeste")
        //adiciona as bolhas ao Sudeste da bolha principal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.maxX + self.mainBubble!.getWidth() * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.maxY + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()
        //self.mainBubble!.restoreDirection()
        //self.multiplier += 1
    } else {
        print("só cheguei até aqui")
    }
}

A variável direction seria responsável por controlar a direção em relação a bolha principal para criar as demais (usaria um multiplicador para criar as bolhas em uma camada externa a primeira camada, mas com isso formaria um desenho parecido com uma estrela) e sem contar que a segunda camada precisaria do dobro de objetos em relação a primeira camada :(.

Estou tentando posicionar os objetos em forma circular de um objeto principal.

1
  • sim mas voce tem que colcoar partes do que ja tentou, e o qe está pensando e fazer.
    – jsbueno
    26/07/2016 às 19:00

1 Resposta 1

1

Para calcular coordenadas de pontos no raio de um circulo você deverá usar as funções seno e cosseno.

Digamos que seu circulo tenha raio 1, e você queria um angulo de 45 graus ( por exemplo ).

Como o código trabalha com radianos: 45 graus é igual a pi/4.

Então x = cos(pi/4)*1 e y = sin(pi/4)*1

Deixando de forma mais genérica:

x = cos(angulo)*altura
y = sin(angulo)*altura

circulo cos e seno

Uns exemplos de como ficaria no seu código:

//adiciona as bolha ao Leste da bolha principal
let x = CGFloat(cos(0)* height) + xCenterMainCircle
let y = CGFloat(sin(0)* height) + yCenterMainCircle

//adiciona as bolha ao Oeste da bolha principal
let x = CGFloat(cos(CGFloat.pi)* height) + xCenterMainCircle
let y = CGFloat(sin(CGFloat.pi)* height) + yCenterMainCircle

Só alguns valores pra você ter como referencia:

0              - Leste
CGFloat.pi/2   - Norte
3*CGFloat.pi/2 - Sul
CGFloat.pi     - Oeste
CGFloat.pi/4   - Nordeste (45º)

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .