• Embedding in scrollviews

    You’ve built a view controller in a storyboard. It started out quite simple, but you added bits here and there and the complexity grew. Now, a new requirement arrives.

    We need a text field. We need another set of content at the bottom.

    All of a sudden, your layout won’t fit on the screen any more. You need it to scroll. That’s no problem, all you need to do is select the root view of the view controller, and choose Embed in… > Scroll View, right?

    Oh. That’s not available when you’re dealing with the root view. So how can you do it? How do you make a non-scrolling view controller scrollable?

    Read more...

  • Scrollviews and stack views

    File under: Every time I do this, I forget how to do it, so I’m writing it down.

    It’s a common occurrence. You have some content in a stack view, and you want it to be scrollable. Maybe the content in the stack can change or you want keyboard avoidance or whatever. How do you set that up in a storyboard without either IB moaning at you, or the views not coming out right?

    Read more...

  • Fun with Sets

    A Set is a collection of unique members. The uniqueness of those members, in a Swift Set, is determined by equality (==). The hashValue is used to improve performance. This means you can only store values in a set that conform to Hashable.

    To combine two sets, you use the union method. Consider the following groups of numbers:

    let someInts = Set([1, 2, 3])
    let someMoreInts = Set([3, 4, 5])
    let allTehInts = someInts.union(someMoreInts)
    

    allTehInts will contain 1, 2, 3, 4 and 5. 3 was in both sets, but sets can only contain unique values, so it doesn’t get included.

    Which 3 is contained in allTehInts? The one from someInts, or the one from someMoreInts? THE ANSWER WILL SHOCK YOU! Or, confuse you, if you’ve just migrated to Swift 3.

    Read more...

  • Pentominoes, part seven: Dropping with intent

    I last left the project with the player being able to pick up and drop tiles on the board, but with no implementation of the game logic that I’d spent all that time building up.

    In this post I’m going to add the game logic in to the drag and drop action.

    My goals are:

    • To show a highlighted “drop zone” on the board as the player moves a tile around. The drop zone will show them where the tile can be dropped
    • To “snap” the tile into the drop zone if the drag is ended while the tile is in a permissible position
    • Otherwise, to snap the tile back to its position at the edge of the board
    • Sort out the messy pickup of tiles on a busy board

    Read more...

  • Pentominoes, part six: Gestures

    After some brief experimentation I decided that live views weren’t good enough to use for playing around with an interactive board and gestures. Also, once you get to dealing with gestures and user interaction with something like this, you really need to be working on a device - things that feel fine with a mouse or trackpad and pointer can be no good at all when you’re working on a device.

    In this post I’m going to talk about adding gesture recognisers and transferring to a full project.

    Read more...

  • Pentominoes, part five: Some drawing

    It’s time to move on from the character-based visualisations of the board and the tiles, and create some views.

    Each tile will be represented with a view, and the board will be a view. Placed tiles will be added as subviews of the board, which will simplify the drawing and positioning logic.

    Read more...

  • Pentominoes, part four: Updating the board

    So far my Board model is smart enough to tell if a Tile can be placed in a specific location. Now I need to think about what happens when the player actually places the tile. How does the board update its model? What needs to happen here?

    Read more...

  • Pentominoes, part three: Placing the first tile

    It’s time to think about some game logic. The first thing a player will do is try to place a Tile on the Board. How can I tell if the move should be allowed?

    To solve this problem I ended up creating a SequenceType and GeneratorType, which is the main focus of this part.

    Read more...

  • Pentominoes, part two: Board

    In part one I went through the process of building the Tile model objects for my Pentominoes puzzle app. In this part I will talk about making the Board, and what I learned about protocols and default implementations in the process.

    Read more...

  • Pentominoes, part one: Tiles

    My daughter and I are members of At-Bristol, a most excellent interactive science centre in Bristol. On a recent visit she was captivated by a “Pentominoes” puzzle. Pentominoes are the twelve possible tile shapes you can make using five squares, joined at their edges. They are a little like tetris shapes, but with five squares instead of four.

    Read more...