Skip to content

Commit

Permalink
add drag gesture.
Browse files Browse the repository at this point in the history
  • Loading branch information
nijino committed Sep 1, 2013
1 parent 5d0d72b commit 121547c
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CircularProgressView.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@optional

- (void)updateProgressViewWithPlayer:(AVAudioPlayer *)player;
- (void)playerDidFinishPlaying;

@end

Expand All @@ -25,6 +26,7 @@
@property (copy, nonatomic) NSString *audioPath;
@property (assign, nonatomic) CGFloat lineWidth;
@property (assign, nonatomic) NSTimeInterval duration;
@property (assign, nonatomic) BOOL playOrPauseButtonIsPlaying;
@property (assign, nonatomic) id <CircularProgressViewDelegate> delegate;

- (id)initWithFrame:(CGRect)frame
Expand Down
57 changes: 55 additions & 2 deletions CircularProgressView.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ @interface CircularProgressView ()<AVAudioPlayerDelegate>
@property (nonatomic) NSTimer *timer;
@property (nonatomic) AVAudioPlayer *player;//an AVAudioPlayer instance
@property (assign, nonatomic) float progress;
@property (assign, nonatomic) CGFloat angle;//angle between two lines

@end

Expand Down Expand Up @@ -60,7 +61,7 @@ - (void)drawRect:(CGRect)rect

//draw background circle
UIBezierPath *backCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2)
radius:CGRectGetWidth(self.bounds) / 2 - self.lineWidth / 2
radius:(CGRectGetWidth(self.bounds) - self.lineWidth) / 2
startAngle:(CGFloat) - M_PI_2
endAngle:(CGFloat)(1.5 * M_PI)
clockwise:YES];
Expand All @@ -71,7 +72,7 @@ - (void)drawRect:(CGRect)rect
if (self.progress) {
//draw progress circle
UIBezierPath *progressCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetWidth(self.bounds) / 2,CGRectGetHeight(self.bounds) / 2)
radius:CGRectGetWidth(self.bounds) / 2 - self.lineWidth / 2
radius:(CGRectGetWidth(self.bounds) - self.lineWidth) / 2
startAngle:(CGFloat) - M_PI_2
endAngle:(CGFloat)(- M_PI_2 + self.progress * 2 * M_PI)
clockwise:YES];
Expand Down Expand Up @@ -124,7 +125,59 @@ - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)f
self.progress = 0;
//self redraw
[self setNeedsDisplay];
[self.delegate playerDidFinishPlaying];
}
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
self.angle = [self angleFromStartToPoint:point];
if (self.player.playing) {
[self pause];
}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
if (CGRectContainsPoint(self.bounds, point)) {
self.angle = [self angleFromStartToPoint:point];
self.player.currentTime = self.player.duration * (self.angle / (2 * M_PI));
[self updateProgressCircle];
}

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
self.player.currentTime = self.player.duration * (self.angle / (2 * M_PI));
if (self.playOrPauseButtonIsPlaying) {
[self play];
}else {
[self updateProgressCircle];
}
}

//calculate angle between start to point
- (CGFloat)angleFromStartToPoint:(CGPoint)point{
CGFloat angle = [self angleBetweenLinesWithLine1Start:CGPointMake(CGRectGetWidth(self.bounds) / 2,CGRectGetHeight(self.bounds) / 2) Line1End:CGPointMake(CGRectGetWidth(self.bounds) / 2,CGRectGetHeight(self.bounds) / 2 - 1) Line2Start:CGPointMake(CGRectGetWidth(self.bounds) / 2,CGRectGetHeight(self.bounds) / 2) Line2End:point];
if (CGRectContainsPoint(CGRectMake(0, 0, CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame)), point)) {
angle = 2 * M_PI - angle;
}
return angle;
}


//caculate angle between 2 lines
- (CGFloat)angleBetweenLinesWithLine1Start:(CGPoint)line1Start
Line1End:(CGPoint)line1End
Line2Start:(CGPoint)line2Start
Line2End:(CGPoint)line2End{
CGFloat a = line1End.x - line1Start.x;
CGFloat b = line1End.y - line1Start.y;
CGFloat c = line2End.x - line2Start.x;
CGFloat d = line2End.y - line2Start.y;
return acos(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));
}
@end
4 changes: 2 additions & 2 deletions CircularProgressView.podspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Pod::Spec.new do |s|
s.name = "CircularProgressView"
s.version = "1.1"
s.version = “1.3
s.summary = "A custom audio circular progress view for iOS."
s.homepage = "https://github.com/nijino/CircularProgressView"
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { "nijino" => "nijino_saki@163.com" }
s.source = { :git => "https://github.com/nijino/CircularProgressView.git",
:tag => "1.1" }
:tag => “1.3 }
s.platform = :ios, '4.3'
s.source_files = 'CircularProgressView.{h, m}'
s.requires_arc = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ - (void)didReceiveMemoryWarning
- (IBAction)clickPlayOrPause:(ToggleButton *)sender {
if (sender.on) {
[self.circularProgressView play];
self.circularProgressView.playOrPauseButtonIsPlaying = YES;
}else {
[self.circularProgressView pause];
self.circularProgressView.playOrPauseButtonIsPlaying = NO;
}
}

Expand All @@ -71,7 +73,11 @@ - (IBAction)clickStop:(id)sender {
#pragma mark Circular Progress View Delegate method
- (void)updateProgressViewWithPlayer:(AVAudioPlayer *)player {
//update timeLabel
self.timeLabel.text = [NSString stringWithFormat:@"%@/%@",[self formatTime:(int)player.currentTime],[self formatTime:(int)player.duration]];
self.timeLabel.text = [NSString stringWithFormat:@"%@/%@",[self formatTime:(int)player.currentTime],[self formatTime:(int)self.circularProgressView.duration]];
}

- (void)playerDidFinishPlaying{
self.playOrPauseButton.on = NO;
}

//format audio time
Expand Down
Binary file modified CircularProgressViewDemo/Default-568h@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CircularProgressViewDemo/Default@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ You can play,pause & stop audio by below methods declared in the header file `Ci
You can find a demo project in this repository.

##Version History
- version 1.3
Add response drag progress view gesture.

- version 1.2
Add initWithCoder method for creating instance from interface builder.

Expand Down
Binary file modified ScreenShot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 121547c

Please sign in to comment.