Pages

Subscribe:

Tuesday, July 19, 2011

How to make a slider button just like iPhone's native Unlock Button


/* In the above screen shot, i've used the UISlider as a alarm triggering object. (as required in an application) */

/* Use your custom images as your requirements. Declare required variables & objects in *.h file. You will need a UIImageView, a UILabel, a UIButton & a UISlider in your NIB file and locate them to desired name.*/

- (void)viewDidLoad {
[super viewDidLoad];
// initialize custom UISlider (you have to do this in viewdidload or applicationdidfinishlaunching.
UIImage *stetchLeftTrack= [[UIImage imageNamed:@"Nothing.png"]
stretchableImageWithLeftCapWidth:30.0 topCapHeight:0.0];
UIImage *stetchRightTrack= [[UIImage imageNamed:@"Nothing.png"]
stretchableImageWithLeftCapWidth:30.0 topCapHeight:0.0];
[mySlider setThumbImage: [UIImage imageNamed:@"SlideToStop.png"] forState:UIControlStateNormal];
[mySlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[mySlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];

}


-(IBAction)LockIt
{
mySlider.hidden = NO;
lockButton.hidden = YES;
Container.hidden = NO;
myLabel.hidden = NO;
myLabel.alpha = 1.0;
UNLOCKED = NO;
mySlider.value = 0.0;
NSString *str = @"The iPhone is Locked!";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Locked" message:str delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}

-(IBAction)fadeLabel
{
myLabel.alpha = 1.0 - mySlider.value;
}

-(IBAction)UnLockIt
{
if (!UNLOCKED)
{
if (mySlider.value == 1.0)
{ // if user slide far enough, stop the operation
// Put here what happens when it is unlocked

mySlider.hidden = YES;
lockButton.hidden = NO;
Container.hidden = YES;
myLabel.hidden = YES;
UNLOCKED = YES;
}
else
{
// user did not slide far enough, so return back to 0 position
[UIView beginAnimations: @"SlideCanceled" context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.35];
// use CurveEaseOut to create "spring" effect
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
mySlider.value = 0.0;
[UIView commitAnimations];

/* if u don't want to use custom animation with desired duration then u can use the following line :

[mySlider setValue:0.0 animated:YES];
*/

}
}
}

Friday, July 15, 2011

Accessing & playing iPod's music player through application

- (void)viewDidLoad {
[super viewDidLoad];

mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAny];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = YES;
mediaPicker.editing = YES;

[self musicPlayerInitialize];

}

-(void)musicPlayerInitialize
{
myPlayer = [MPMusicPlayerController iPodMusicPlayer];

[myPlayer setQueueWithQuery: [MPMediaQuery songsQuery]];
[myPlayer setRepeatMode:MPMusicRepeatModeAll];

[myPlayer beginGeneratingPlaybackNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeTitle)
name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:myPlayer];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleExternalVolumeChanged:)
name:MPMusicPlayerControllerVolumeDidChangeNotification
object:myPlayer];


}


-(void)changeTitle
{
MPMediaItem *item = [myPlayer nowPlayingItem];
NSString *titleStr = [item valueForProperty:MPMediaItemPropertyTitle];
lblSongTitle.text = titleStr;
NSLog(@"Now Playing Song Name: %@",titleStr);
}


-(IBAction)playButtonPressed
{
if(myPlayer.playbackState == MPMusicPlaybackStatePaused || myPlayer.playbackState == MPMusicPlaybackStateStopped)
{
[myPlayer play];
[btnMusicPlay setImage:[UIImage imageNamed:@"pause_btn.png"] forState:UIControlStateNormal];
}
else if(myPlayer.playbackState == MPMusicPlaybackStatePlaying)
{
[myPlayer pause];
[btnMusicPlay setImage:[UIImage imageNamed:@"play_btn.png"] forState:UIControlStateNormal];
}
[self changeTitle];
}

-(IBAction)nextButtonPressed
{
[myPlayer skipToNextItem];
[self changeTitle];
}

-(IBAction)prevButtonPressed
{
[myPlayer skipToPreviousItem];
[self changeTitle];
}

-(IBAction)volumeChange:(id)sender
{
[[NSUserDefaults standardUserDefaults] setFloat:myVolumeSlider.value forKey:@"musicVolume"];
myPlayer.volume = myVolumeSlider.value;
NSLog(@"volume changing: %f",myVolumeSlider.value);
}

- (void)handleExternalVolumeChanged:(id)notification {
// self.volumeSlider is a UISlider used to display music volume.
// self.musicPlayer.volume ranges from 0.0 to 1.0.
[myVolumeSlider setValue:myPlayer.volume animated:YES];
}


# pragma mark -
# pragma mark media picker Delegate

-(void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
[self dismissModalViewControllerAnimated:YES];
}

-(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[tempArray addObject:mediaItemCollection];

MPMediaItemCollection *tempMediaItem = [tempArray objectAtIndex:0];
[appDelegate.myPlayer setQueueWithItemCollection:tempMediaItem];
[appDelegate.myPlayer play];
[self dismissModalViewControllerAnimated:YES];
}

-(IBAction)playlistButtonPressed
{
[self presentModalViewController:mediaPicker animated:YES];
}

Countdown Timer with remaining time & custom label (hr:min:sec)

-(IBAction)timerStartButtonPressed
{
if(!isStarted)
{
isStarted = TRUE;
isPaused = FALSE;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFunction:) userInfo:nil repeats:YES];
}
}

-(IBAction)timerPauseButtonPressed
{
if(!isPaused)
{
isStarted = FALSE;
isPaused = TRUE;
[timer invalidate];
}
}



-(void)timerFunction:(NSTimer *)theTimer
{
second = second-1;
remSecond = remSecond + 1;

if(hour >= 0)
{
if (second < 0)
minute = minute-1;

if(remSecond > 59)
remMinute = remMinute + 1;

if(minute < 0)
hour = hour - 1;

if(remMinute > 59)
remHour = remHour + 1;

if(minute < 0 && second < 0 && hour < 0)
{
strSecond = @"00";
strMinute = @"00";
strHour = @"00";

NSString *tempTestDuration = [[NSUserDefaults standardUserDefaults] valueForKey:@"restDuration"];
NSArray *tempArray = [tempTestDuration componentsSeparatedByString:@":"];

remHour = [[tempArray objectAtIndex:0] intValue];
remMinute = [[tempArray objectAtIndex:1] intValue];
remSecond = [[tempArray objectAtIndex:2] intValue];


isStarted = FALSE;
isPaused = TRUE;

[timer invalidate];
timer = nil;

if(isTestRunning && remainingSet < totalSet)
{
[self resetRest];
[self timerStartButtonPressed];
}
else if(!isTestRunning && remainingSet < totalSet)
{
[self resetTest];
if([[NSUserDefaults standardUserDefaults] boolForKey:@"isNewSongInEachTestOn"] == YES)
[appDelegate.myPlayer skipToNextItem];
[self timerStartButtonPressed];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Finish!!!" message:@"Time up." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}

return;
}

if (second>=0)
{
if (second<10)
strSecond = [NSString stringWithFormat:@"0%d", second];
else
strSecond = [NSString stringWithFormat:@"%d", second];
}
else
{
second=59;
strSecond = [NSString stringWithFormat:@"%d", second];
}

if(remSecond <= 59)
{
if (remSecond<10)
strRemSecond = [NSString stringWithFormat:@"0%d", remSecond];
else
strRemSecond = [NSString stringWithFormat:@"%d", remSecond];
}
else
{
remSecond=0;
strRemSecond = [NSString stringWithFormat:@"0%d", remSecond];
}

if(minute >= 0)
{
if (minute<10)
strMinute = [NSString stringWithFormat:@"0%d", minute];
else
strMinute = [NSString stringWithFormat:@"%d", minute];
}
else
{
minute = 59;
strMinute = [NSString stringWithFormat:@"%d",minute];
}

if(remMinute <= 59)
{
if (remMinute<10)
strRemMinute = [NSString stringWithFormat:@"0%d", remMinute];
else
strRemMinute = [NSString stringWithFormat:@"%d", remMinute];
}
else
{
minute = 0;
strMinute = [NSString stringWithFormat:@"0%d",remMinute];
}


if (hour<10)
strHour = [NSString stringWithFormat:@"0%d", hour];
else
strHour= [NSString stringWithFormat:@"%d", hour];

if (remHour<10)
strRemHour = [NSString stringWithFormat:@"0%d", remHour];
else
strRemHour= [NSString stringWithFormat:@"%d", remHour];

NSString *tempStr = [NSString stringWithFormat:@"%@:%@:%@",strHour,strMinute,strSecond];
NSString *tempStrRem = [NSString stringWithFormat:@"%@:%@:%@",strRemHour,strRemMinute,strRemSecond];
NSLog(@"tempStr: %@",tempStr);

if([tempStr characterAtIndex:0] == '0')
{
strHour = [NSString stringWithFormat:@"%c",[tempStr characterAtIndex:1]];
tempStr = [NSString stringWithFormat:@"%@:%@:%@",strHour,strMinute,strSecond];
NSLog(@"tempStr: %@",tempStr);

if([tempStr characterAtIndex:0] == '0')
{
tempStr = [NSString stringWithFormat:@"%@:%@",strMinute,strSecond];
NSLog(@"tempStr: %@",tempStr);

if([tempStr characterAtIndex:0] == '0')
{
strMinute = [NSString stringWithFormat:@"%c",[tempStr characterAtIndex:1]];
tempStr = [NSString stringWithFormat:@"%@:%@",strMinute,strSecond];
NSLog(@"tempStr: %@",tempStr);

if([tempStr characterAtIndex:0] == '0')
{
tempStr = [NSString stringWithFormat:@"0:%@",strSecond];
NSLog(@"tempStr: %@",tempStr);
}
}
}
}

if([tempStrRem characterAtIndex:0] == '0')
{
strRemHour = [NSString stringWithFormat:@"%c",[tempStrRem characterAtIndex:1]];
tempStrRem = [NSString stringWithFormat:@"%@:%@:%@",strRemHour,strRemMinute,strRemSecond];
NSLog(@"tempStr: %@",tempStrRem);

if([tempStrRem characterAtIndex:0] == '0')
{
tempStrRem = [NSString stringWithFormat:@"%@:%@",strRemMinute,strRemSecond];
NSLog(@"tempStr: %@",tempStrRem);

if([tempStrRem characterAtIndex:0] == '0')
{
strRemMinute = [NSString stringWithFormat:@"%c",[tempStrRem characterAtIndex:1]];
tempStrRem = [NSString stringWithFormat:@"%@:%@",strRemMinute,strRemSecond];
NSLog(@"tempStr: %@",tempStrRem);

if([tempStrRem characterAtIndex:0] == '0')
{
tempStrRem = [NSString stringWithFormat:@"0:%@",strRemSecond];
NSLog(@"tempStr: %@",tempStrRem);
}
}
}
}


lblTimerDisplay.text = tempStr;
lblRemainingTimeDisplay.text = [NSString stringWithFormat:@"Elapsed Time %@",tempStrRem];
}
else
{
strSecond = @"00";
strMinute = @"00";
strHour = @"00";

[timer invalidate];
}

}