Reply to topic  [ 79 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6  Next
Dipping the toe in - Swift, Xcode, adaptive layouts 
Author Message
What's a life?
User avatar

Joined: Thu Apr 23, 2009 6:27 pm
Posts: 12251
Reply with quote
Someone on Twitter has just asked me if it's for sale on the App Store.

_________________
All the best,
Paul
brataccas wrote:
your posts are just combo chains of funny win

I’m on Twitter, tweeting away... My Photos Random Avatar Explanation


Wed Nov 19, 2014 11:48 am
Profile
What's a life?
User avatar

Joined: Thu Apr 23, 2009 6:27 pm
Posts: 12251
Reply with quote
Another question - thought I’d ask before I completely disrupt what I have. I may want to change the look of the clock. I get that this is easy -I have a pile of CA Layers, and all I need to do is change the images, as well as the centre of rotation.

So (and this is pseudocode), I have this:

currently in ClockView.swift :

Code:
class ClockView: UIView {
//sets up CALayers here
var secondHandLayer = CALayer()
...

func awakeFromNib() {
// Sets up the clock face - loads images, draws bits

// Set the position of the hands

// Start the animation

// Various functions to rotate the hands & drawing arcs

}

}


What I think I should be doing (just so I can separate the design/layout of the clock) is this:

in clockView.swift :
Code:
class ClockView: UIView {
//sets up CALayers here
var secondHandLayer = CALayer()
...

func awakeFromNib() {
drawClockFace1()

// Set the position of the hands

// Start the animation

// Various functions to rotate the hands & drawing arcs

}

}


and then in clockface1.swift:
Code:
func drawClockFace1(){
// all the clock face drawing stuff that was originally in awakeFromNib
// loading images into the CALayers defined in clockView.swift
}


My reasoning being that each clockface#.swift file will effectively draw and layout each design of face, but will not be involved in the running of the clock. I was thinking of using separate files just to keep things easy for me to follow, and this should allow the user to select a clock face and when they do, the appropriate function would be called, revising the clock. However, would variables like the CALayer secondHandLayer be visible to functions elsewhere in the app?

This is a bit of forward thinking on my part. I’d like to include an option for the user to change the clock face, so I need to prepare for that by separating the style from the mechanism.

_________________
All the best,
Paul
brataccas wrote:
your posts are just combo chains of funny win

I’m on Twitter, tweeting away... My Photos Random Avatar Explanation


Wed Nov 19, 2014 12:48 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Hmm... yeah, you definitely want to separate the "look" from the "logic". However, what I think might be better is to have something like a "Configuration" class. I'm just typing off the top of my head atm so I haven't thought it through fully.

Create a class with properties like...

Code:
UIImage *clockFaceImage
UIImage *secondHandImage
UIImage *hourHandImage
UIImage *dayNightImage
...

CGPoint secondHandCenterOfRotation
CGPoint hourHandCenterOfRotation
CGPoint dayNightCenterOfRotation
...

//you could even have
CGPath dayNightMaskPath
...


Now in your actual clockView you can give it a property like...

Code:
ClockConfiguration *config


And when you "draw" your clock and create the layers etc you can do...

Code:
CALayer secondHandLayer // create the layer...
secondHandLayer.image = self.config.secondHandImage;
secondHandLayer.anchorPoint = self.config.secondHandCenterOfRotation;
etc...


So now you can create instances of your Config class to create different "looks" for the clock.

This way you only have one ClockView class and you can make it look different by using different instances of a single ClockConfig class.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Nov 19, 2014 5:13 pm
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 6:27 pm
Posts: 12251
Reply with quote
Hmm - I'll have to think about that. Possibly some experimentation tomorrow. And Googling. Lots of Googling, I expect.

_________________
All the best,
Paul
brataccas wrote:
your posts are just combo chains of funny win

I’m on Twitter, tweeting away... My Photos Random Avatar Explanation


Wed Nov 19, 2014 6:03 pm
Profile
What's a life?
User avatar

Joined: Thu Apr 23, 2009 6:27 pm
Posts: 12251
Reply with quote
Ok - so my approach just isn’t going to work (damn). This is doe to the fact that Swift doesn’t like this kind of mucking about:

code fragments in ClockView.swift:
Code:
class ClockView : UIView {
var myVariable = "Some text"
[ ... rest of code ... ]
}


in ViewController.swift:
Code:
override func viewDidLoad() {
        super.viewDidLoad()
        myText = ClockView.myVariable
}


So, I found that you nseed to set up protocols because variables can’t be accessed in this way otherwise. Like this.

In Configuration.swift:
Code:
import Foundation
import UIKit
import QuartzCore

protocol configClass {
    var clockfaceImage: CGImageRef { get set }
}

class configuration : configClass {
    var clockfaceImage: CGImageRef
    init() {
        clockfaceImage = UIImage(named: "Clock Face" )!.CGImage
    }
}


and in ViewColtroller (in ViewDidLoad - but I expect AwakeFromNib should be used):

Code:
var clockface1 = configuration()
var myImage = clockface1.clockfaceImage
clockface1.clockfaceImage = UIImage(named: "Clock Face" )!.CGImage


This compiles and appears to return an image reference, so it appears to work. I’ll have to think about how I move this on, as it seems rather odd. I’m not sure I fully understand Oli’s method for setting the config property, or how I’d change the look of the clock “mid flight”. I have some ideas, but I need to try them out.

_________________
All the best,
Paul
brataccas wrote:
your posts are just combo chains of funny win

I’m on Twitter, tweeting away... My Photos Random Avatar Explanation


Thu Nov 20, 2014 10:27 am
Profile
What's a life?
User avatar

Joined: Thu Apr 23, 2009 6:27 pm
Posts: 12251
Reply with quote
I really shoudl be doing somehting else. I’ve chased this round a few times, and not got any further. Here’s the problem.

1 - I have a Configuration.swift file that looks like this:
Code:
import Foundation
import UIKit
import QuartzCore

protocol configClass {
    var clockfaceImage: CGImageRef { get set }
}

class config : configClass {
    var clockfaceImage: CGImageRef
    init() {
        clockfaceImage = UIImage(named: "Clock Face" )!.CGImage
    }
}


In ClockView.swift I have this:
protocol ClockViewClass {
func drawFace(configuration: config)
}

Code:
class ClockView: UIView , ClockViewClass {
    var myConfiguration: config!
    [... snipped code ...]

    func drawFace( configuration: config ) {
        println("Drawing clock face")
        faceLayer.contents=(configuration.clockfaceImage)
        faceLayer.setNeedsDisplay()
    }
}


And in ViewController.swift I have this:
Code:
var clockface1 = config()
class ViewController: UIViewController {
[...snipped code ...]
    override func viewDidLoad() {
        super.viewDidLoad()
        clockface1.clockfaceImage = UIImage(named: "Clock Face 2" )!.CGImage

        var ItsMyClock = ClockView()
        ItsMyClock.drawFace( clockface1 )
    }
}


The problem is this: when ItsMyClock.drawface( clockface1 ) is called, nothing happens. I am expecting the clock face to change to a new one. I expect that is because I am creating a new instance of ClockView and that’s the problem.

However, if I do this:
Code:
ClockView.drawFace( clockface1 )

I get a compiler error: 'config' is not convertible to 'ClockView'

What I am trying to do is pass the configuration to the drawFace method in ClockView.

If I do this:
Code:
ClockView.drawFace()

with drawFace defined like this:
Code:
func drawFace()


I get the following compiler error: Missing argument for parameter #1 in call

I expect I’ve got this arseaboutface. I expect you can see what I’m trying to do. Really, I’m just trying to call drawFace() in ClockView. I’m using protocols because that seems to be the way to get access to variables etc. within Classes.

_________________
All the best,
Paul
brataccas wrote:
your posts are just combo chains of funny win

I’m on Twitter, tweeting away... My Photos Random Avatar Explanation


Thu Nov 20, 2014 2:00 pm
Profile
What's a life?
User avatar

Joined: Thu Apr 23, 2009 6:27 pm
Posts: 12251
Reply with quote
Damn - I feel I’m really close, but things still bug me.

ClockView is now doing this to set up the variables (I was getting errors I could not get around when trying to use init() ):

Code:
class ClockView: UIView {
   var faceLayer: CALayer = CALayer()
[...]
        class func drawFace( configuration: config ) {
                println("Drawing clock face")
        }
}


This works - when I call ClockView.drawFace( somevariable ) the println is called. Hooray! Noting here that it’s called in VideDidLoad() in View.Controller, and it runs after ClockView’s AwakeFromNib() so all the variables SHOULD be in place.

However, if I change drawFace to:

Code:
   class func drawFace( configuration: config ) {
      println("Drawing clock face")
//      self.faceLayer.frame = CGRect(x:0 , y:0 , width: 10, height: 10)
      self.faceLayer.contents = (configuration.clockfaceImage)
   }


I get the error "ClockView.type does not have a member named 'faceLayer'" which confuses me, as I’ve set it up previously. Same if I remove the “self”.

_________________
All the best,
Paul
brataccas wrote:
your posts are just combo chains of funny win

I’m on Twitter, tweeting away... My Photos Random Avatar Explanation


Thu Nov 20, 2014 4:23 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
It's hard to tell without seeing it. Would it be possible to share the project or put something on gist so I can have a look?

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Nov 20, 2014 11:48 pm
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 6:27 pm
Posts: 12251
Reply with quote
Fogmeister wrote:
It's hard to tell without seeing it. Would it be possible to share the project or put something on gist so I can have a look?


Thanks. I’ll DM you with a DropBox URL.

_________________
All the best,
Paul
brataccas wrote:
your posts are just combo chains of funny win

I’m on Twitter, tweeting away... My Photos Random Avatar Explanation


Fri Nov 21, 2014 9:44 am
Profile
What's a life?
User avatar

Joined: Thu Apr 23, 2009 6:27 pm
Posts: 12251
Reply with quote
Oliver’s helped me with my clock face problem, and given me more things to think about as a result. One of them was to do with the aspect ratio of hands passing between the various configuration settings. It seems that I was heading in the right direction, with my inexperience to help me.

Anyway, as evidence of my success, I’ll post the draft of the second clock face running in the simulator.

ImageClick for large view - Uploaded with Skitch

It needs some tidying up (esp some of the numbers), but you get the idea. It’s actually based on a clock I own, which was very likely from Woolworths - my mum says she remembers them. So it’s a regular guy’s art deco clock, not a posh one.

Thanks, Oliver! :D

_________________
All the best,
Paul
brataccas wrote:
your posts are just combo chains of funny win

I’m on Twitter, tweeting away... My Photos Random Avatar Explanation


Sat Nov 22, 2014 11:52 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Awesome! Looking good now :-)

It's made me think about how I would approach other similar apps. Very cool though. :-)

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Sun Nov 23, 2014 1:50 am
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 6:27 pm
Posts: 12251
Reply with quote
Fogmeister wrote:
Awesome! Looking good now :-)

It's made me think about how I would approach other similar apps. Very cool though. :-)


Today’s progress:
• Positioning hands (no longer confined to the middle of the dial)
• Can now toggle between dial 1 and dial 2 - noting that when I add dial 3, I should be able to up the count limit and it should just work™

_________________
All the best,
Paul
brataccas wrote:
your posts are just combo chains of funny win

I’m on Twitter, tweeting away... My Photos Random Avatar Explanation


Sun Nov 23, 2014 6:37 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Awesome :-) how did you do the positioning?

Also, yeah, you should be able to create an array of configurations to switch between.

Also, something to blow your mind. You can now do things like put the clockView into a collection view or table so the user can pick from the configurations by seeing them in action :-)

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Sun Nov 23, 2014 9:43 pm
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 6:27 pm
Posts: 12251
Reply with quote
Fogmeister wrote:
Awesome :-) how did you do the positioning?

In a nutshell - added a CGPoint to the Configuration, and in ClockView amended your positioning code so that it doesn’t centre any more. I also created a modifier variable which scales the numbers down (or up) assuming that 768 is the native size. I am working all my clock faces in a 768x768 document, so I can get the dimensions form that. I plug those numbers in, and the app does the scaling for me.

Fogmeister wrote:
Also, yeah, you should be able to create an array of configurations to switch between.

I was considering something along those lines. I need another clock face so I can test against three.

Fogmeister wrote:
Also, something to blow your mind. You can now do things like put the clockView into a collection view or table so the user can pick from the configurations by seeing them in action :-)

Hmm - I was wondering how I could present a selection matrix. Right now, you tap the button, and it hop from one to the next, and back to the beginning again. I’d like some kind of selection tool where you pan/scroll between them and tap the one you want. I guess you’d throw an instance of ClockView in each cell/pane, and tell it which one to show. Something to ponder over.

_________________
All the best,
Paul
brataccas wrote:
your posts are just combo chains of funny win

I’m on Twitter, tweeting away... My Photos Random Avatar Explanation


Sun Nov 23, 2014 11:01 pm
Profile
What's a life?
User avatar

Joined: Thu Apr 23, 2009 6:27 pm
Posts: 12251
Reply with quote
Oh - as an aside, my wife still doesn’t get WHY I’m doing this. Who, she asks, would actually WANT a Metric Clock? I told her that someone asked me if it was available to buy on the app store. She said that “that’s one person”. :? I don’t think she fully understands this. I’ll remind her of this when she shows me the chicken she’s animating in Toon Boom Studio. :lol:

She does like the square clock, though.

_________________
All the best,
Paul
brataccas wrote:
your posts are just combo chains of funny win

I’m on Twitter, tweeting away... My Photos Random Avatar Explanation


Sun Nov 23, 2014 11:04 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 79 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6  Next

Who is online

Users browsing this forum: No registered users and 102 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

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.