Online Book Reader

Home Category

iOS Recipes - Matt Drance [27]

By Root 263 0

}

}

By responding to the state changes, we can set the image on the Play button to match the correct action—for example, Play when paused, and Pause when playing.

InfinitePlayback/PRPMusicViewController.m

- (void)playbackStateChanged: (id) notification {

MPMusicPlaybackState playerState = [myPlayer playbackState];

if (playerState == MPMusicPlaybackStatePaused) {

[playPauseButton setImage:[UIImage imageNamed:

@"mediumPlayButton.png"]

forState:UIControlStateNormal];

} else if (playerState == MPMusicPlaybackStatePlaying) {

[playPauseButton setImage:[UIImage imageNamed:

@"mediumPauseButton.png"]

forState:UIControlStateNormal];

} else if (playerState == MPMusicPlaybackStateStopped) {

[playPauseButton setImage:[UIImage imageNamed:

@"mediumPlayButton.png"]

forState:UIControlStateNormal];

[myPlayer stop];

}

}

To draw the attention of the user to the newly available control, it’s helpful to provide a little animation when we add the control to the view. The effect we use here is to “pop” the view onto the screen by using a scale transformation that gives the impression of the control zooming out toward us. When the music control is dismissed, we use the reverse effect, reducing the scale of the view to zero, so that the control zooms away from us. We do not need to specify a duration for the animation because the default value of 0.25 works nicely in this case. We only need to set the delegate for the ZoomOutView method because, in that case, we need to remove the view from its SuperView once the animation is complete.

InfinitePlayback/PRPMusicViewController.m

- (void)ZoomInView {

self.view.layer.transform = CATransform3DMakeScale(1, 1, 1);

CABasicAnimation *anim = [CABasicAnimation animation];

anim.keyPath = @"transform.scale";

anim.fromValue = [NSNumber numberWithFloat:0];

anim.toValue = [NSNumber numberWithFloat:1.0];

[self.view.layer addAnimation:anim forKey:@"scaleIn"];

}

- (void)ZoomOutView {

CABasicAnimation *anim = [CABasicAnimation animation];

anim.keyPath = @"transform.scale";

anim.fromValue = [NSNumber numberWithFloat:1.0];

anim.toValue = [NSNumber numberWithFloat:0];

anim.removedOnCompletion = NO;

anim.fillMode = kCAFillModeBoth;

anim.delegate = self;

[self.view.layer addAnimation:anim forKey:@"scaleOut"];

}

- (void)animationDidStop:(CABasicAnimation *)anim finished:(BOOL)flag {

[self.view.layer removeAllAnimations];

[self.view removeFromSuperview];

}

Now you have a reasonably functional album track player. You could still enhance the functionality by adding more controls, displaying more of the track metadata, allowing the option to scan ahead or backward in a track, or using a slider to control track playback position. All of those features follow the same general pattern established here and would not be too difficult for you to implement.

Recipe 13 Have Fun with Autoscrolling Text Views

Problem

Trying to make help screens or any other text view interesting can be a challenge. You want to add a little something to give your app a touch of style or to demonstrate that you, the developer, don’t necessarily take yourself too seriously.

Solution

The sample app for this recipe, scrollingCredits, will make you either smile or grimace (see Figure 17, Star-themed scrolling credits ). Though it’s meant to be quite lighthearted, this example contains some useful techniques. The three elements that are worth discussing in detail are using a transform for distorting the text, using Core Animation to make the text view autoscroll, and using the AVAudio framework to play back some music.

Figure 17. Star-themed scrolling credits

* * *

Working with 3D transforms can be a little challenging, but in general we create the matrices that produce the transformations using the library of methods that are defined for us by Core Animation, for example, scale (CATransform3DMakeScale), rotate (CATransform3DMakeRotation), and translate (CATransform3DMakeTranslation). We can also directly access the individual elements of the matrices to create some really interesting

Return Main Page Previous Page Next Page

®Online Book Reader