iOS Question Rotating a circle on a circle path

tufanv

Expert
Licensed User
Longtime User
Hello,

I also need advice on another thing. Please see the pic below.


5xoqoz.jpg



I need to rotate a circle picture or a circle around a circle track conitnously. ( you can think of world turning around the sun on a track ) . What is the best and easiest way to do it ?

TY
 

narek adonts

Well-Known Member
Licensed User
Longtime User
Attached small project demonstrating your issue.
I use my code module in the project to get and set the center of a view, but if you dont like it you can do the same with setting the left and top props.
 

Attachments

  • Rotate.zip
    3.2 KB · Views: 223
Upvote 0

strat

Active Member
Licensed User
Longtime User
An imageview's moving along circle track is here:


B4X:
Sub Activity_Create(FirstTime As Boolean)
    angle=0
    radius=40%x
End Sub



This is main code: X and Y variables keep ImageView's circular coordinates.
You should edit to work in B4I, add variables, timer etc.

B4X:
Sub timer1_Tick
    angle=angle+1
    x=CosD(angle)*radius+50%x
    y=SinD(angle)*radius+50%y
    ImageView1.Left=x
    ImageView1.Top=y
End Sub


Edit : This is almost same as narek adonts sample. I didn't see it before post this.
 
Last edited:
Upvote 0

tufanv

Expert
Licensed User
Longtime User
see attached project.
It use CAAnimation to be more smooth that the one with the timer.

Narek
I have made a small change on the code ( added a panel and initialized the circle on the new panel. btw i dont know why but i had to change the left and top values while adding the circle to panel to a minus otherwise there are 2 circles ,1 is staying at its posiion and other one is circling . you can try it by changing the : paneloyun.AddView(pnlCircle,-100,-50,20,20)to its actual positions on your code)

anyway, When i use a canvas to draw a circle exactly on the circle path with this code:

B4X:
dim cek as point
cek.X=CircleCenter.x
cek.Y=CircleCenter.y
  cv.DrawCircle(cek.X,cek.Y,150,Colors.Black,False,1)
     cv.Refresh

Now, the spinning circle has to spin just above the circle created with the canvas because their center is same and their radius is same but they are not. Am i doing stg wrong or ?

You can check the example.

TY
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
you should not initialize canvas in application_start event.

put cv.Initialize(paneloyun) in Page_resize event
Dear Narek

I have one last question for you. I can't get the top,left values of the rotating circle with a timer. It always shows the top,left values of the initialized values. While it is moving i cant get the coordinates of current position via log(pnlcircle.top) for example. Is there any way i can get it ?
 
Upvote 0

narek adonts

Well-Known Member
Licensed User
Longtime User
You can not. During any animation the views coordinates are the last coordinates and also you can not click, ... on the view.
You need to use a timer for that. I will upload my new class CADisplayLink which is also like a timer but it ticks on each screen Hz refresh. So you will be able to use it as I timer but you will have smooth animations
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
You can not. During any animation the views coordinates are the last coordinates and also you can not click, ... on the view.
You need to use a timer for that. I will upload my new class CADisplayLink which is also like a timer but it ticks on each screen Hz refresh. So you will be able to use it as I timer but you will have smooth animations

Watiing for it Thank You !
 
Upvote 0

narek adonts

Well-Known Member
Licensed User
Longtime User
At the moment you can use this function to retrieve the actual Rect of a view (event during animation).

B4X:
Sub GetCurrentRect (view As View) As Rect
Dim no As NativeObject=view
no =no.RunMethod("layer",Null).RunMethod("presentationLayer",Null).RunMethod("frame",Null)
Dim r As Rect=OBJC.CGRectToRect(no)
Return r
End Sub

You should also update the OBJC module (attached)

Narek
 

Attachments

  • OBJC.bas
    1.8 KB · Views: 204
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Thank you, I have now updated the .bas an added the code to my project.
Now do i add under timer_tick getcurrentrect(x) . I tried pnlcircle,panelyoun but they did not work
At the moment you can use this function to retrieve the actual Rect of a view (event during animation).

B4X:
Sub GetCurrentRect (view As View) As Rect
Dim no As NativeObject=view
no =no.RunMethod("layer",Null).RunMethod("presentationLayer",Null).RunMethod("frame",Null)
Dim r As Rect=OBJC.CGRectToRect(no)
Return r
End Sub

You should also update the OBJC module (attached)

Narek
 
Upvote 0
Top