My iOS Apps iOS Software Mac OS Software Classic Software HyperCard Software |
MacTidingsNU » Sork Software » Text In Motion |
Updated 14-02-17 |
|
Text In Motion
One of the cool new features in iOS 7 is motion effects, a technique that makes it easier to add depth to your apps UI. It is used for the parallax effetcs in the new OS. However, a number of people complaining about nausea from the movements suggests that the effects should be used with caution and perhaps as an option only for those with a tendency to motion sickness. UIInterpolatingMotionEffect *horizontalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis]; // set the min and max values here [theView addMotionEffect:horizontalMotionEffect];
Notice how the motion is applied as an animation to the NSDictionary* dict = @{@"center" : [NSValue valueFromCGPoint:CGPointMake(3.4, 1.2)], @"layer.shadowOffset.x : @(-1.1) };
Which is cool but riddled with three errors: valueFromCGPoint: should be valueWithCGPoint:, shadowOffset takes a CGSize so the following property should be a width, not x, and there is a missing closing " after the property.
My subclass with the notification code use the following code. Please note that the parameter
- (NSDictionary *)keyPathsAndRelativeValuesForViewerOffset:(UIOffset)viewerOffset {
// Apply a static factor to the movement offsets
CGFloat xOffset = 11 * viewerOffset.horizontal;
CGFloat yOffset = 11 * viewerOffset.vertical;
// This method returns a value associated with property key paths
NSValue *offsetPoint = [NSValue valueWithCGPoint:CGPointMake(xOffset, yOffset)];
// Notify our VC about a move action and its offsets packed into a dictionary as a point value
NSValue *offsets = [NSValue valueWithCGPoint:CGPointMake(viewerOffset.horizontal, viewerOffset.vertical)];
NSDictionary *didMove = @{@"Move" : self, @"Offset" : offsets};
[[NSNotificationCenter defaultCenter] postNotificationName:MoveNotification object:self userInfo:didMove];
// The view
Then in my VC the notification code updates the title view shadow offset property as a point: - (void)deviceMoved:(NSNotification *)notification { // Get the move from the notification userInfo NSValue *theMove = [[notification userInfo] objectForKey:@"Offset"]; CGPoint thePoint = [theMove CGPointValue]; // Update the drawing with the new offsets self.titleView.offsets = thePoint; [self.titleView setNeedsDisplay]; } The title and its offset will update every time a motion sends a notification. Here is the beginning of the drawing code with some calculations for the shadow offsets: - (void)drawRect:(CGRect)rect { // The offsets come from our view controllers notification deviceMoved: // Modify the offsets with the same factor as the view movement CGFloat xAdjustment = 11 * self.offsets.x; CGFloat yAdjustment = 11 * self.offsets.y; // The basic shadow offset is 3.3 UIColor* shadow = shadowColor; CGSize shadowOffset = CGSizeMake(3.3 - xAdjustment, 3.3 - yAdjustment); CGFloat shadowBlurRadius = 6;
I will not show the rest of the long-winded CG code here, you can visit it in the sample project if you wish. Version list Version 1.0 first release. Jan 2014. Downloads Xcode 5 project for the motion: TextInMotion.zip (61 kB) E-mail your thoughts on the example to: Putta is the simple game with a twist (or two). |