Custom UIProgressView drawing weirdness - objective-c

I am trying to create my own custom UIProgressView by subclassing it and then overwrite the drawRect function.
Everything works as expected except the progress filling bar. I can't get the height and image right.
The images are both in Retina resolution and the Simulator is in Retina mode.
The images are called: "progressBar#2x.png" (28px high) and "progressBarTrack#2x.png" (32px high).
CustomProgressView.h
#import <UIKit/UIKit.h>
#interface CustomProgressView : UIProgressView
#end
CustomProgressView.m
#import "CustomProgressView.h"
#implementation CustomProgressView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 16);
UIImage *progressBarTrack = [[UIImage imageNamed:#"progressBarTrack"] resizableImageWithCapInsets:UIEdgeInsetsZero];
UIImage *progressBar = [[UIImage imageNamed:#"progressBar"] resizableImageWithCapInsets:UIEdgeInsetsMake(4, 4, 5, 4)];
[progressBarTrack drawInRect:rect];
NSInteger maximumWidth = rect.size.width - 2;
NSInteger currentWidth = floor([self progress] * maximumWidth);
CGRect fillRect = CGRectMake(rect.origin.x + 1, rect.origin.y + 1, currentWidth, 14);
[progressBar drawInRect:fillRect];
}
#end
The resulting ProgressView has the right height and width. It also fills at the right percentage (currently set at 80%). But the progress fill image isn't drawn correctly.
Does anyone see where I go wrong?

Looks like you're reassigning self.frame in -drawRect.
I think you want something like this:
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGRect bounds = self.bounds ;
UIImage *progressBarTrack = [ UIImage imageNamed:#"progressBarTrack"] ;
[ progressBarTrack drawInRect:bounds ] ;
UIImage *progressBar = [[UIImage imageNamed:#"progressBar"] resizableImageWithCapInsets:(const UIEdgeInsets){ 4.0f, 4.0f, 5.0f, 4.0f } ] ;
CGRect fillRect = CGRectInset( bounds, 2.0f, 2.0f ) ;
fillRect.width = floorf( self.progress * maximumWidth );
[progressBar drawInRect:fillRect];
}
How to create your own progress view overriding UIView instead of UIProgressView
#interface ProgressView : UIView
#property float progress ;
#end
#implementation ProgressView
#synthesize progress = _progress ;
-(id)initWithFrame:(CGRect)frame
{
if (( self = [ super initWithFrame:frame ] ))
{
self.layer.needsDisplayOnBoundsChange = YES ;
}
return self ;
}
-(void)drawRect
{
// see code above
}
-(void)setProgress:(float)progress
{
_progress = progress ;
[ self setNeedsDisplay ] ;
}
#end

Related

Not getting UIImageView Rounded Border while Added Multiplier for UIImageViwe

In Storyboard I applied Multiplier for height and width to UIImageView then I just want to rounded border so I used below code its not work for all iPhones.
_profileImgView.clipsToBounds = YES;
_profileImgView.layer.backgroundColor = color.CGColor;
_profileImgView.layer.cornerRadius =_profileImgView.frame.size.width/2;
_profileImgView.layer.borderColor = [UIColor colorWithRed:253.0/255.0 green:182.0/255.0 blue:43.0/255.0 alpha:100].CGColor;
_profileImgView.layer.borderWidth = 5.0f;
Since your corner radius depends on your frame size, you need to update it whenever the frame size changes. If you are using storyboards for your design, you will get the frame size that is in the design when viewDidLoad is called. If the frame size differs for different devices, you will get the final size at a later point in time in the views layoutSubviews or possibly the view controller's viewDidLayoutSubviews.
My suggested solution is to sub-class UIImageView and put the specifics for the image view in awakeFromNib and layoutSubviews, then use this class instead of UIImageView where appropriate.
// CircularImageView.h
#import <UIKit/UIKit.h>
#interface CircularImageView : UIImageView
#end
// CircularImageView.m
#implementation CircularImageView
- (void)awakeFromNib {
[super awakeFromNib];
self.clipsToBounds = YES;
self.layer.borderColor = [UIColor colorWithRed:253.0/255.0 green:182.0/255.0 blue:43.0/255.0 alpha:100].CGColor;
self.layer.borderWidth = 5.0f;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.layer.cornerRadius = self.frame.size.width / 2;
}
#end
implement this code in same Viewcontroller class it is work for me
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
_profileImgView.clipsToBounds = YES;
_profileImgView.layer.backgroundColor = color.CGColor;
_profileImgView.layer.cornerRadius =_profileImgView.frame.size.width/2;
_profileImgView.layer.borderColor = [UIColor colorWithRed:253.0/255.0 green:182.0/255.0 blue:43.0/255.0 alpha:100].CGColor;
_profileImgView.layer.borderWidth = 5.0f;
}

NSPopover show relative to NSStatusItem

This is my code:
if #available(OSX 10.10, *) {
if let b = statusItem.button {
popover.showRelativeToRect(b.bounds, ofView: b, preferredEdge: .MinY)
}
} else {
}
The else block is for OS X Mavericks because NSStatusItem.button is not available. Is there a simple way of showing the popover relative to the status item? If not, is it possible to show the popover in the center of the screen instead without the arrow?
before you had access to the statusitem button you had to provide your own view. Then all works the same
to retain the original behaviour, draw a custom view that looks like a status item ;)
e.g.
#interface DDQuickMenuStatusItemView : NSView
#property(weak) NSStatusItem *item;
//...
#end
#implementation DDQuickMenuStatusItemView
//...
- (void)drawRect:(NSRect)dirtyRect {
NSImage *image = nil;
if(self.item) {
[self.item drawStatusBarBackgroundInRect:self.bounds withHighlight:NO];
image = self.item.image;
}
if(image) {
NSRect r = self.bounds;
r.size = [image size];
r = [self.class centerRect:r inRect:self.bounds];
r = [self centerScanRect:r];
[image drawInRect:r fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
}
}
#pragma mark -
+ (CGRect)centerRect:(CGRect)rect inRect:(CGRect)inRect
{
CGRect result = rect;
result.origin.x = inRect.origin.x + (inRect.size.width - result.size.width)*0.5f;
result.origin.y = inRect.origin.y + (inRect.size.height - result.size.height)*0.5f;
return result;
}
#end
Note that the view is a sample and not production ready ;)

CAEmitterLayer not visible in my View Controller

I'm trying to add a simple particle effect overlay to one of my UIViewController instances. I've followed a couple of tutorials I found, but neither of them deal with view controllers and their storyboards are just a view out on its own with no controller, which is confusing.
Here's my code, I'm just trying to figure out why I can't see the particle effects. What am I missing? The view itself is definitely there and added (if I change its color or something I can see it), it's just empty, and isn't showing any particle effects. The image file referenced is definitely in the project and target, so what else have I done wrong here? Do I need to add the CAEmitterLayer to rainView somehow? The tutorials didn't offer any help on this part!
RainfallOverlay.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface RainfallOverlay : UIView {
CAEmitterLayer *rainEmitter;
}
#end
RainfallOverlay.m
#import "RainfallOverlay.h"
#implementation RainfallOverlay
- (void) awakeFromNib
{
rainEmitter = (CAEmitterLayer *) self.layer;
rainEmitter.emitterPosition = CGPointMake(160, 100);
rainEmitter.emitterSize = CGSizeMake(10, 10);
rainEmitter.renderMode = kCAEmitterLayerAdditive;
CAEmitterCell *rain = [CAEmitterCell emitterCell];
rain.birthRate = 200;
rain.lifetime = 2.0;
rain.lifetimeRange = 1.5;
rain.color = [[UIColor colorWithRed: 0.2 green: 0.4 blue: 0.8 alpha: 0.1] CGColor];
rain.contents = (id) [[UIImage imageNamed: #"Particles_rain.png"] CGImage];
rain.name = #"rain";
rain.velocity = 150;
rain.velocityRange = 100;
rain.emissionRange = M_PI_2;
rain.emissionLongitude = 0.025 * 180 / M_PI;
rain.scaleSpeed = 0;
rain.spin = 0.5;
rainEmitter.emitterCells = [NSArray arrayWithObject: rain];
}
#end
ViewController.m viewDidLoad
RainfallOverlay *rainView = [[RainfallOverlay alloc] initWithFrame: CGRectMake(0, 0, 320, 250)];
[rainView setUserInteractionEnabled: NO];
[self.view bringSubviewToFront: rainView];
[self.view addSubview: rainView];
There are 2 problems:
awakeFromNib would only be called if the view is loaded from a nib file. In your case, you have to implement initWithFrame.
You have to override layerClass to that self.layer returns a CAEmitterLayer. Just casting the layer to CAEmitterLayer does not work.
So your RainfallOverlay implementation should look like this:
+ (Class)layerClass
{
return [CAEmitterLayer class];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
rainEmitter = (CAEmitterLayer *) self.layer;
// ... remaining setup
}
return self;
}

Implementing a Simple Line Graph in iOS

What would be the best way to implament a simple line graph inside my iPad app. Im only looking to plot 4-8 points and the graph does need to be fancy looking. HTML5 will be good enough to use for what I need.
How would I go about implementing HTML code to plot a line graph?
I have been looking at using Google Charting Tools, but any other suggestions that I could use?
Here is a very basic class that will give you the basic line. It takes an NSArray of NSNumbers for the Y axis. It should provide a good starting point for you as it is as simple as it gets. You might consider adding labels to the points and axis markers to give a sense of scale.
Header:
#import <UIKit/UIKit.h>
#interface SOGenericGraphView : UIView {
NSArray *yValues;
}
#property (strong, atomic) NSArray *yValues;
#end
Implementation:
#import "SOGenericGraphView.h"
#implementation SOGenericGraphView
#synthesize yValues;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[super drawRect:rect];
CGRect insetRect = CGRectInset(self.frame, 10, 15);
double maxSpeed = [[yValues valueForKeyPath:#"#max.doubleValue"] doubleValue];
CGFloat yRatio = insetRect.size.height/maxSpeed;
CGFloat xRatio = insetRect.size.width/(yValues.count-1);
UIBezierPath *sparkline = [UIBezierPath bezierPath];
for (int x = 0; x< yValues.count; x++) {
CGPoint newPoint = CGPointMake(x*xRatio + insetRect.origin.x, insetRect.size.height - (yRatio*[[yValues objectAtIndex:x] doubleValue] - insetRect.origin.y));
if (x == 0) {
[sparkline moveToPoint:newPoint];
}
else {
[sparkline addLineToPoint:newPoint];
}
}
[[UIColor redColor] set];
[sparkline stroke];
}
#end

How can I use an IBAction from inside my controller to draw a circle in my custom view?

I draged out a generic view and hooked it up to my circleView.m. I then dragged out a round rect button on top of that view and hooked up an IBAction to it. As of right now, when the view loads the circle is automatically drawn onto the screen. What I would like to do is draw the circle on the screen only when the button is pressed using drawRect or some other draw method. Here is my code:
drawCircleViewController.h
#import <UIKit/UIKit.h>
#interface drawCircleViewController : UIViewController
#end
drawCircleViewController.m
#import "drawCircleViewController.h"
#import "circleView.h"
#interface drawCircleViewController()
#property (nonatomic, weak) IBOutlet circleView *circleV;
#end
#implementation drawCircleViewController
#synthesize circleV = _circleV;
- (IBAction)buttonPressedToDrawCircle:(id)sender {
// This is action I want to use to draw the circle in my circleView.m
}
#end
circleView.h
#import <UIKit/UIKit.h>
#interface circleView : UIView
#end
circleView.m
#import "circleView.h"
#implementation circleView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawCircleAtPoint:(CGPoint)p
withRadius:(CGFloat)radius
inContext:(CGContextRef)context
{
UIGraphicsPushContext(context);
CGContextBeginPath(context);
CGContextAddArc(context, p.x, p.y, radius, 0, 2*M_PI, YES);
CGContextStrokePath(context);
UIGraphicsPopContext();
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint midpoint;
midpoint.x = self.bounds.origin.x + self.bounds.size.width / 2;
midpoint.y = self.bounds.origin.y + self.bounds.size.height / 2;
#define DEFAULT_SCALE 0.90
CGFloat size = self.bounds.size.width / 2;
if (self.bounds.size.height < self.bounds.size.width) size = self.bounds.size.height / 2;
size *= DEFAULT_SCALE;
CGContextSetLineWidth(context, 5.0);
[[UIColor blueColor] setStroke];
[self drawCircleAtPoint:midpoint withRadius:size inContext:context];
}
#end
With what you have there, the simplest approach would probably be to make your circle view hidden and show it when the button is pressed.
Otherwise, you can keep a BOOL in your view to denote whether the button has been tapped and check that during drawRect: (and use setNeedsDisplay to trigger changes).