
First of all, make sure you have the most current version of the PocketTouch framework. It will always be posted here:
http://www.touchrepo.com/source/Pock....framework.zip on the TouchRepo. Take that and add it to all your iPhone SDKs in Xcode, in the PrivateFrameworks directory.
Link to Xcode project: http://www.touchrepo.com/SampleCode/...ource_Code.zip
Link to Precompiled Application (just drop it into /Applications) so you can see the app before you download the Xcode project: http://www.touchrepo.com/SampleCode/...pplication.zip
Source Code:
TrackInfoView.h
Objective C Code:
#import <UIKit/UIKit.h>
@interface TrackInfoView : UIView {
UILabel *album;
UILabel *artist;
UILabel *song;
}
@property (nonatomic, retain) UILabel *album;
@property (nonatomic, retain) UILabel *artist;
@property (nonatomic, retain) UILabel *song;
@end
TrackInfoView.m
Objective C Code:
#import "TrackInfoView.h"
@implementation TrackInfoView
@synthesize album, artist, song;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self layoutSubviews];
}
return self;
}
- (void)layoutSubviews {
self.bounds = CGRectMake(self.bounds.origin.x, self.superview.bounds.origin.y, self.bounds.size.width, self.superview.bounds.size.height);
/*- Alternatively, set the bounds in Interface Builder. It. by default, reduces the height 12 px and offsets the origin.y 6 px -*/
CGRect frame = self.frame;
CGRect artistRect = CGRectMake(0, 0, frame.size.width, frame.size.height / 3);
CGRect songRect = CGRectMake(0, frame.size.height / 3, frame.size.width, frame.size.height / 3);
CGRect albumRect = CGRectMake(0, (frame.size.height / 3) * 2, frame.size.width, frame.size.height / 3);
album = [[UILabel alloc] initWithFrame:albumRect];
album.backgroundColor = [UIColor clearColor];
album.font = [UIFont boldSystemFontOfSize:12];
album.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
album.shadowOffset = CGSizeMake(0, -1);
album.text = @"Album";
album.textAlignment = UITextAlignmentCenter;
album.textColor = [UIColor lightGrayColor];
artist = [[UILabel alloc] initWithFrame:artistRect];
artist.backgroundColor = [UIColor clearColor];
artist.font = [UIFont boldSystemFontOfSize:12];
artist.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
artist.shadowOffset = CGSizeMake(0, -1);
artist.text = @"Artist";
artist.textAlignment = UITextAlignmentCenter;
artist.textColor = [UIColor lightGrayColor];
song = [[UILabel alloc] initWithFrame:songRect];
song.backgroundColor = [UIColor clearColor];
song.font = [UIFont boldSystemFontOfSize:12];
song.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
song.shadowOffset = CGSizeMake(0, -1);
song.text = @"Song";
song.textAlignment = UITextAlignmentCenter;
song.textColor = [UIColor whiteColor];
[self addSubview:album];
[self addSubview:artist];
[self addSubview:song];
}
- (void)dealloc {
[album release];
[artist release];
[song release];
[super dealloc];
}
@end
TEST_MEDIACONTROL_APPViewController.h
Objective C Code:
#import <PocketTouch/PocketTouch.h>
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
#import "TrackInfoView.h"
@interface TEST_MEDIACONTROL_APPViewController : UIViewController {
IBOutlet UIBarButtonItem *nextButton;
IBOutlet UIBarButtonItem *prevButton;
IBOutlet UIImageView *albumArt;
UIImageView *reflectionView;
IBOutlet UINavigationBar *controlBar;
IBOutlet TrackInfoView *infoView;
PTMusicController *musicController;
}
@property (nonatomic, retain) IBOutlet UIBarButtonItem *nextButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *prevButton;
@property (nonatomic, retain) IBOutlet UIImageView *albumArt;
@property (nonatomic, retain) IBOutlet UINavigationBar *controlBar;
@property (nonatomic, retain) IBOutlet TrackInfoView *infoView;
- (IBAction)nextButtonPressed:(id)sender;
- (IBAction)prevButtonPressed:(id)sender;
- (void)manageArtwork;
- (void)updateNavigationBarTrackInfo;
#pragma mark Reflection Methods
CGImageRef CreateGradientImage(int gradientWidth, int gradiendHeight);
- (UIImage *)reflectionImage:(UIImageView *)fromImage withHeight:(NSUInteger)height;
@end
TEST_MEDIACONTROL_APPViewController.m
Objective C Code:
#import "TEST_MEDIACONTROL_APPViewController.h"
#define kDefaultReflectionFraction 0.25
#define kDefaultReflectionOpacity 0.75
@implementation TEST_MEDIACONTROL_APPViewController
@synthesize nextButton, prevButton, albumArt, controlBar, infoView;
- (void)dealloc {
[musicController release];
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
musicController = [[PTMusicController alloc] init];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGRect reflectionRect = self.albumArt.frame;
reflectionRect.size.height = reflectionRect.size.height * kDefaultReflectionFraction;
reflectionRect = CGRectOffset(reflectionRect, 0, self.albumArt.frame.size.height);
reflectionView = [[UIImageView alloc] initWithFrame:reflectionRect];
reflectionView.alpha = kDefaultReflectionOpacity;
[self.view addSubview:reflectionView];
[self manageArtwork];
[self updateNavigationBarTrackInfo];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ? NO : YES);
}
- (IBAction)nextButtonPressed:(id)sender {
[musicController playNextSong];
[self updateNavigationBarTrackInfo];
[self manageArtwork];
}
- (IBAction)prevButtonPressed:(id)sender {
[musicController playPreviousSong];
[self updateNavigationBarTrackInfo];
[self manageArtwork];
}
- (void)manageArtwork {
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setDuration:0.66];
[animation setTimingFunction:UIViewAnimationCurveEaseInOut];
[animation setType:@"twist"];
[animation setSubtype:kCATransitionFromTop];
[animation setFillMode: @"extended"];
[animation setRemovedOnCompletion:NO];
[[self.albumArt layer] addAnimation:animation forKey:@"twistAnimation"];
///[[reflectionView layer] addAnimation:animation forKey:@"twistAnimation"];
self.albumArt.image = [musicController getAlbumArtWithSize:CGSizeMake(320, 320)];
reflectionView.image = [self reflectionImage:self.albumArt withHeight:self.albumArt.bounds.size.height * kDefaultReflectionFraction];
}
- (void)updateNavigationBarTrackInfo {
NSArray *infoArray = [NSArray arrayWithArray:[musicController readTrackInfo]];
infoView.artist.text = [infoArray objectAtIndex:0];
infoView.album.text = [infoArray objectAtIndex:2];
infoView.song.text = [infoArray objectAtIndex:1];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark Reflection Methods
CGImageRef CreateGradientImage(int gradientWidth, int gradiendHeight) {
CGImageRef theCGImage = NULL;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef gradientBitmapContext = CGBitmapContextCreate(nil, gradientWidth, gradiendHeight, 8, 0, colorSpace, kCGImageAlphaNone);
CGFloat colors[] = {0.0, 1.0, 1.0, 1.0};
CGGradientRef grayScaleGradient = CGGradientCreateWithColorComponents(colorSpace, colors, NULL, 2);
CGColorSpaceRelease(colorSpace);
CGPoint gradientStartPoint = CGPointZero;
CGPoint gradientEndPoint = CGPointMake(0, gradiendHeight);
CGContextDrawLinearGradient(gradientBitmapContext, grayScaleGradient, gradientStartPoint, gradientEndPoint, kCGGradientDrawsAfterEndLocation);
theCGImage = CGBitmapContextCreateImage(gradientBitmapContext);
CGContextRelease(gradientBitmapContext);
return theCGImage;
}
- (UIImage *)reflectionImage:(UIImageView *)fromImage withHeight:(NSUInteger)height {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate (nil, fromImage.bounds.size.width, height, 8, 0, colorSpace, (kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst));
CGColorSpaceRelease(colorSpace);
CGFloat translateVertical= fromImage.bounds.size.height - height;
CGContextTranslateCTM(bitmapContext, 0, -translateVertical);
CALayer *layer = [fromImage layer];
[layer renderInContext:bitmapContext];
CGImageRef bitmapImage = CGBitmapContextCreateImage(bitmapContext);
CGContextRelease(bitmapContext);
CGImageRef gradientMaskImage = CreateGradientImage(1, height);
CGImageRef reflectionImage = CGImageCreateWithMask(bitmapImage, gradientMaskImage);
CGImageRelease(bitmapImage);
CGImageRelease(gradientMaskImage);
UIImage *theImage = [UIImage imageWithCGImage:reflectionImage];
CGImageRelease(reflectionImage);
return theImage;
}
#pragma mark Touch Methods
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
NSArray *allTouches = [touches allObjects];
if (CGRectContainsPoint([self.albumArt frame], [[allTouches objectAtIndex:0] locationInView:self.view])) {
[musicController playOrPauseSong];
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setDuration:0.66];
[animation setTimingFunction:UIViewAnimationCurveEaseInOut];
[animation setType:@"tubey"];
[animation setSubtype:kCATransitionFromTop];
[animation setFillMode: @"extended"];
[animation setRemovedOnCompletion:NO];
[[self.albumArt layer] addAnimation:animation forKey:@"tubeyAnimation"];
}
}
@end
All other source is unchanged from the source created with Xcode's "View-Based Application" template.