View unanswered posts | View active topics
It is currently Fri Jul 04, 2025 2:43 am
Dipping the toe in - Swift, Xcode, adaptive layouts
Author |
Message |
paulzolo
What's a life?
Joined: Thu Apr 23, 2009 6:27 pm Posts: 12251
|
I now have a hand that rotates once - and it does so in 86.,4 seconds, which is a decimal minute. Next to get it to repeat (every rotation, the aim is to set the minute and hour hands). I’m using CAKeyframeAnimation as I was a smooth animation for the second hand (all posh clocks & watches have smooth running second hands). Why CAKeyframeAnimation? Well, it seems that I have to keyframe a full rotation because (in radians), 0 degrees and 360 degrees are the same, so nothing moves! My next job is to rummage around for setCompletionBlock to see how I can call a function to set the hands and restart the animation again. Why is is that a lot of Swift info is either in Japanese, or out of date (ie old Beta stuff that seems to be obsolete)? Bloody annoying.
|
Mon Nov 10, 2014 4:07 pm |
|
 |
jonbwfc
What's a life?
Joined: Thu Apr 23, 2009 7:26 pm Posts: 17040
|
I can't answer the first one, but the second is because it's not very long out of beta, surely? If you want to be at the cutting edge 
|
Mon Nov 10, 2014 5:15 pm |
|
 |
Fogmeister
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 7:35 pm Posts: 6580 Location: Getting there
|
Download an app called dash. It's a really good way to view docs. Also, swift is just the language. You're still using all the same apis as in Objective-C just with a new language.
|
Mon Nov 10, 2014 7:36 pm |
|
 |
paulzolo
What's a life?
Joined: Thu Apr 23, 2009 6:27 pm Posts: 12251
|

Getting close now  Got some maths that seems not to be working (that’s probably me), as well as getting the hands in the right position (ie centre of the clock face, rather than at some position in the vague neighbourhood - again, probably some wonky maths going on. But what I DO have is a clock that rotates the hour hand correctly (the calculation for the minute hand seems a little off, so it’s doing nothing), and the second hand moves around, and every decimal minute the function to set the hands is called, and the second had is rotated again. So happy with what I’ve got now, but to do: 1 - Get the minute hand rotation calculated correctly 2 - Position the clock face dead centre of the screen (I expect I’ll have to look at the interface builder more and figure out how that will do this) 3 - Place hands correctly on the face 4 - Get it to respond/redraw nicely when the device is rotated 6 - Better graphics (I’ve got some, but they seem a little fuzzy in the simulator) 7 - Chiming (including those clunk noises clocks make a few minutes before the chimes start) It’s coming together. Slowly, and painfully, but it’s getting there. Thanks for the continued guidance, guys. Some chump will ask me if there’ll be an Apple Watch version next..... 
|
Tue Nov 11, 2014 10:23 am |
|
 |
Fogmeister
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 7:35 pm Posts: 6580 Location: Getting there
|
Hehe, nice work. Let me know if I can help any more  Also, if you're looking for a good Swift resource then Apple created a book called "Introduction to Swift" you can get it through iBooks. If you are planning on making an Apple Watch version then it should be fairly easy. You've probably done all the work necessary already.
|
Tue Nov 11, 2014 10:58 am |
|
 |
paulzolo
What's a life?
Joined: Thu Apr 23, 2009 6:27 pm Posts: 12251
|
 I’m trying a few more things out, but I may call for a bit more help if I need it. I have to see what I can find out myself before I ask. I expect I’ve done a few things wrong that I don’t know about yet.  I’ll have a look for the book too. I think the majority of the battle isn’t Swift itself, but rather how Swift makes thing happen, usually through various API calls, as well as some thing sin XCode that don’t quite make sense because they are different to what I am used to.
|
Tue Nov 11, 2014 3:54 pm |
|
 |
jonbwfc
What's a life?
Joined: Thu Apr 23, 2009 7:26 pm Posts: 17040
|
There's a second book in the series (also free) called 'Using Swift with Cocoa and Objective C'. That sounds like more your requirement although I suspect they'll assume you've read the first one already.
|
Tue Nov 11, 2014 7:00 pm |
|
 |
paulzolo
What's a life?
Joined: Thu Apr 23, 2009 6:27 pm Posts: 12251
|

Woohoo! Click for large view - Uploaded with SkitchThis represents where I am at the moment - a screenshot of the Simulator running as an iPhone 6. The green square is the ClockView object, which sizes according to the screen size, so it’s always the full width/height (depending on what’s smaller). The clock is drawn inside this object, and the hands are now properly positioned (I believe) and they rotate around their pivot point. The time is calculated every metric minute, and the minute hand moves rather nicely each time (instead of just jumping). I’ve not got much further now - the next step will be to test positions by entering arbitrary times and seeing how the hands point. I thought, however, that this is a good time to show what I have as I’ve actually got where I wanted to be with it. Oh - it seems to survive rotation without any intervention on my part  Oddly, I could not get the final size of the ClockView object. All the sizes and coordinates returned are based on the settings in the storyboard, not on the dimensions when the screen has been drawn. For now, this is fine, but I’ve managed to totally confuse myself with the coordinates within ClockView. I got there eventually, but they seemed to defy my thinking about where the origin was (I expect, again, it’s based on the numbers in the StoryBoard, and not the screen drawn clock). Also, it’s crashing into the sides - I’ll be making it a little smaller eventually. I’ll eventually need to hide the status bar at the top of the screen. I don’t really want to confuse people by showing the real time  Anyway, progress. I’m happy. 
|
Wed Nov 12, 2014 9:52 am |
|
 |
paulzolo
What's a life?
Joined: Thu Apr 23, 2009 6:27 pm Posts: 12251
|
I should add what”s known as “complications”. http://www.bbc.co.uk/news/blogs-magazin ... r-30002096I like the look of the day/night thing - maybe I should add that in. It would be a masked rotating image. Still, more to do before I approach that barrel of laughs.
|
Wed Nov 12, 2014 12:28 pm |
|
 |
Fogmeister
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 7:35 pm Posts: 6580 Location: Getting there
|
Ooh, that would be cool.
For the star field it would be fairly easy to create. Create a layer that contains the entire star field image. The create another layer CAShapeLayer and set the path to be an oval.
Now you can use the oval layer as a mask on the star field layer. All you need to do is rotate the star field layer and the visible part (the bit in the oval) will change.
|
Wed Nov 12, 2014 1:13 pm |
|
 |
paulzolo
What's a life?
Joined: Thu Apr 23, 2009 6:27 pm Posts: 12251
|
I had a think about that, and once I had realised that the rotating image had to be layer WITHIN the masked layer, I ended up with this: Click for large view - Uploaded with SkitchIt looks a bit washed out as I have a layer that simulates glass. This looks OK for the web version, but it may be a bit too much for the iOS version, so it may go. The “complication” I was referring to was the day/night indicator you got on some older clocks and watches. In mine, it’s just reflecting the hour hand - remember, in a mechanical clock, they were mostly decorative. Anyway, the mask (which is wrong - it needs to be a semi circle, exposing the top half - I’ll bother about that in a while) is doing its job, though I’d like an inner shadow (which I’m going to have to think about - I expect another layer, blurred, inside the masked layer).
|
Thu Nov 13, 2014 4:39 pm |
|
 |
paulzolo
What's a life?
Joined: Thu Apr 23, 2009 6:27 pm Posts: 12251
|
Oh - and I’ve just noticed one other thing. The images for this project are varying sizes depending on whether its a iPhone or an iPad. Up until now, I have been working out what size I need based on the screen size, and choosing them by name. So I have Hour Hand 100 for iPhones and Hour Hand 320 for iPads. I now notices that image collections can be set for iPad, iPhone, Universal (the default). I expect that means that I can have an iPad collection called “Hour Hand” and an iPhone collection also called “Hour Hand”, and the correct image will be selected for me? Or is that for compiling to specific devices? Oh - maybe not - looking at it a bit more, seems that I can add devices to each image collection. Hmm. Will experiment. EDIT: As I suspected - it’s one image reference with variations depending on the device. That’s handy to know, and means I don’t have to muck about with varying screen sizes in the code 
|
Thu Nov 13, 2014 4:44 pm |
|
 |
Fogmeister
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 7:35 pm Posts: 6580 Location: Getting there
|
Awesome, I like the day night thing. For paths I always work with UIBezierPath. It has much more convenient methods. For the semicircle you should be able to do something like this... Also, offset the center of the rotating layer from the center of the mask layer. Looks like you have them the same at the moment. Offset it and the rotation will still work fine will just push the "wrong" image further out of the masked area.
|
Thu Nov 13, 2014 5:31 pm |
|
 |
paulzolo
What's a life?
Joined: Thu Apr 23, 2009 6:27 pm Posts: 12251
|
The mask is just a test path, and it's all wrong anyway. I'll look at offsets after I've worked out the mask path I need (thanks for your example - I'll try that later).
|
Thu Nov 13, 2014 6:09 pm |
|
 |
Fogmeister
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 7:35 pm Posts: 6580 Location: Getting there
|
Also, if don't think you need to put the day night image into the mask layer. You should just be able to set the mask as the mask property on it.
|
Thu Nov 13, 2014 8:13 pm |
|
|
Who is online |
Users browsing this forum: No registered users and 6 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum
|
|