One of my favourite parts of Xamarin is it’s flexibility. Using Xamarin and Xamarin.Forms you can actually jump between Native API’s, Forms APIs and Html5 (generated at locally or on the server). This flexibility has enabled me to develop a range of different solutions for clients based on their situation, I wasn’t sandboxed to Native […]
I am adding an Apple Watch app to my latest project and needed a way to communicate with the parent app on the phone. I looked at Wormhole for this type of communication, and I was happy to see that there is a port, appropriately named Wormhole Sharp. Wormhole and Wormhole Sharp use App Groups to create a way for apps to shared data via the iOS file system.
Download and build Wormhole Sharp from here and add a reference to both the parent and extension app projects.
Open the Entitlements.plist in the parent app project and select Enable App Groups. Add a new entry in the App Group section.
Do the same in the extension project and make sure to name the group the same
Apple requires naming groups by starting with ‘group’ and then using their common reverse naming style
After setting the group in both the extension, and the parent app, go and set the “Custom Entitlements” to the Entitlements.plist for both the parent and extension respectively. Do this by double clicking on the project in the solution pane.
This is the most critical part, without this it will not work and you will spend a day trying to figure out what is going on.
Next we will learn about communicating between App Extensions and the Parent App using Wormhole Sharp. I spent way more time getting this configured to work than I did with setting up back and forth communication.
One thing I miss in iOS, which Android has, is auto expanding labels (and other views). Android has the
wrap_content layout option, which works really well to flow a view.
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/currency_symbol" android:textAppearance="@style/TextAppearance.AppCompat.Large" android:textColor="@android:color/darker_gray"/>
iOS was lacking that – or so I thought. In the past, I’d had to use code to measure the size of a block of text, then set the frame and adjust other things.
var size = new NSString("Text here").StringSize(font, 9999, UILineBreakMode.CharacterWrap); label.Frame = new CGRect(label.Frame.Origin, size);
Autolayout changes things a bit. It’s all declarative-y and designer-y, so I prefer to keep things like that inside the storyboard designer as much as possible. I recently learned how to do this, tho: the >= (or <=) constraint.
In this case, I want the stop identifier (WW) to resize based on it’s content. It could be blank, or it could have one or 2 letters. I’m using the size of WW – about the maximum width – as the largest size, and allowing it to resize as needed. All the other constraints which are dependant on it flow from there.
The only constraint I need to manually adjust is the space between the WW and the following label (“7” in the image – “75m North West”), as I want to remove it – set it to zero – if the stop id is blank.
This works for vertical height too. Set it to be
greater than or equal to the minimum height – usually one line of text, which is around 21 points – and set the
number of lines to zero. The label will now take as much vertical space as needed.
This is not something which is obvious, at least it wasn’t to me, but it solves one of the big blockers I had with both Autolayout, and iOS in general – dynamic layout, especially of text.
Join Xamarin at our first Mobile Dev + Test conference in San Diego, CA from April 14-16, 2015. Key members from the Xamarin team will be available to answer your questions, discuss your apps and projects, and show you what’s new in Xamarin Test Cloud. James Montemagno, Xamarin Developer Evangelist, will talk about mobile’s impact […]
The post Join Xamarin at Mobile Dev + Test in San Diego, CA appeared first on Xamarin Blog.
Beacons, small pieces of Bluetooth-transmitting hardware, have simplified the ability to create context-aware apps for mobile devices. Given the powerful potential beacons have for impacting the world, we wanted to hear how the Xamarin community would use beacons to improve their lives in fun, creative, and practical ways. After much deliberation, we are pleased to […]
If you’re new to game development, you may be wondering what framework is right for you. Luckily for C# and F# developers, Xamarin supports a wide range of powerful options like SceneKit and SpriteKit on iOS, as well as cross-platform frameworks including OpenTK, CocosSharp, and MonoGame. For developers just getting started, we recently published several […]
As I described in a previous serie of post, vector drawables (and their animated counterparts) are one of the most powerful new visual elements introduced with Lollipop (and soon as a support lib it seems).
In this post, I’m gonna show you how we can achieve the following transition animation by solely using vector drawables and Android’s built-in widget state tracking mechanism:
To build an animation like this, you have to prepare the adequate stage. Afterwards, it’s just a matter of moving each individual pieces of that stage when needed.
In the above video, we can split the final visual in a couple of different parts.
Some like the center dot and the upper-half donut are static pieces, they never move. The two white thin rectangles, symbolizing the hands on a watch, are individual moving parts. The sun, moon and exclamation mark symbols are the varying state of the visual depending on the input.
Now, that’s only the visible part of the puzzle. If you look closely at the animation you can notice that everytime there is a state transition the right hand seems to be erasing the visible time symbol before the new one fade in.
This is where things get interesting because to be able to achieve this effect you have to get creative with the Z-ordering of your stage.
To understand this better, below is a rendering of all the layers of the visual:
The rectangle you see covering the half-bottom part of the picture is not an artifact. It’s painted with the same color than the overall window background thus acting as a cheap but effective hiding mask.
If I conceal this rectangle, here is what’s lurking behind: the other half of the visible top donut
To create the aformentioned effect, we will be moving that half-bottom donut to cover the top one, synchronizing it with the right clock hand. When not in use, it will be safely tucked behind our hiding rectangle.
Because vector drawables have a simple bottom to top drawing hierarchy, for this to work correctly we have declare pieces in a specific order:
- top half donut
- the 3 time symbols
- bottom half donut
- hiding rectangle
- the 2 hands white rectangle
Based on our animation needs, we then split this further into actual vector drawable
<group /> elements to arrive at the following shape (path data removed for brevity, you can download all the XML pack at the end of the article):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
State tracking widget
Before we dwelve into some more animation tricks, we first need to investigate how to make widget to represent our triple state because there is nothing in the default framework that allow this.
The good news is that, as I showed in a previous blog, it’s very easy to create a custom, state-aware widget which is what we are going to do with this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
By setting our state transition drawable as the background of this view, it will automatically react to state changes and animate accordingly. Using the normal state tracking mechanism also means that interruption mid-animations are gracefully handled for us.
One more trick
At this stage, I won’t go over every details of the individual animation definitions, they all tweak standard things like alpha and rotation on each piece of our stage. Nonetheless, you can read my animated vector drawable entry for more details on how they work.
I will only show a little trick that is useful when setting up more complex stages like this: zero-duration animators.
Take the following example that animates the bottom-half donut (to create the progressive hiding effect):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
The idea is that right after we have covered the entire upper-half, we want to immediately tuck it back under our hiding rectangle. This is because we then fade-in the new time symbol which would be hidden too otherwise.
Using zero-duration animators allow you to do just that by applying direct values to animated properties. The fact that there is a from attribute doesn’t matter much but since those properties are write only it will prevent object animator from complaining about missing accessors.
I use this in a couple of places to reset the stage state (since vector drawable instances are reused across transitions), quickly move away elements or hide them from view when they are not useful anymore.
You can download the XML definitions and widget source here.
Bug alert: there is currently a parsing bug where the animated vector drawable inflater will greedily read the entire document given to it. This causes an issue if you declare them inline in your animated state transition XML file because it will corrupt your entire state by reading everything instead of stopping at its corresponding end tag. The workaround is to always declare them in their own XML files like I did.