Drawings on the Screen

Discussion in 'iOS Programming' started by yygyt, Feb 14, 2011.

  1. yygyt macrumors newbie

    Joined:
    Feb 4, 2011
    #1
    Hello all,

    I want to draw several circles of different colors on the screen(Assume that I have a class that draws circle on the screen taking a point and a color as parameters). The problem is that they won't not fit into the screen. I have a question in relation to this problem.

    I decided to use uitableview in order to deal with the fitting problem but I don't know how to draw circles on a cell.

    It may be straightforward, but I am new to this platform.

    Any help will be appreciated.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    How are you attempting to draw these circles? And what leads you to believe that using a tableview is how to ensure they fit on-screen?
     
  3. yygyt, Feb 15, 2011
    Last edited: Feb 16, 2011

    yygyt thread starter macrumors newbie

    Joined:
    Feb 4, 2011
    #3
    I had a class called CircleView that extended UIView and in the drawRect method it calls the following function
    Code:
    -(void)drawCircleAtPoint:(CGPoint)p withRadius:(CGFloat)rad andPercentage:(int)percent inContext:(CGContextRef)cont{
    	UIGraphicsPushContext(cont);
    	CGContextBeginPath(cont);
    	CGContextAddArc(cont, p.x, p.y, rad, 0, 2*M_PI, NO);
    	CGContextSetLineWidth(cont, 1.0);
    	[[UIColor colorWithRed:0.19 green:0.31 blue:0.31 alpha:0.65] setFill];
    	CGContextFillPath(cont);
    	UIGraphicsPopContext();
            ...
    }
    
    But using this approach I could draw only one circle on the screen. I want to draw 30 circles. I want to display something like the following pic

    [​IMG]

    For this purpose I thought that I could use a table view. Of course I can accept any other reasonable suggestions.

    Sorry for my bad English and I don't know if I am clear enough. If not, please warn me.
     
  4. yygyt thread starter macrumors newbie

    Joined:
    Feb 4, 2011
    #4
    Any idea that helps me get started is appreciated. Just a general one like "don't use that, use "x", instead" will help me a lot.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    What happens if you call drawCircleAtPoint: multiple times (with different parameters, of course) from your drawRect method?
     
  6. yygyt, Feb 17, 2011
    Last edited: Feb 17, 2011

    yygyt thread starter macrumors newbie

    Joined:
    Feb 4, 2011
    #6
    By changing the point parameter that describes the center of the circle I have multiple circles on the screen.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Post your code.
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    All you gotta do is write your own UIView subclass that draws as many circles as you like wherever. Put one of those views in a scrollview.
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Which is what you wanted, correct? I'm not sure I understand what your issue is now. Please elaborate.
     
  10. yygyt thread starter macrumors newbie

    Joined:
    Feb 4, 2011
    #10
    You are right. That is what I want. The reason I started asking from the beginning is that I wasn't sure if I was going in the right direction.

    Thanks for the replies.

    Can you tell me how I can add scroll view to the following piece of code.

    CircleView.h
    Code:
    #import <UIKit/UIKit.h>
    
    @class CircleView;
    @protocol CircleViewDelegate
    -(NSDictionary *)rJInfo:(CircleView *)requestor;
    @end
    
    @interface CircleView : UIView {
    	id <CircleViewDelegate> delegate;
    }
    @property (assign) id <CircleViewDelegate> delegate;
    @end
    

    CircleView.m
    Code:
    #import "CircleView.h"
    #define START_ARC -0.5*M_PI
    
    @implementation CircleView
    
    @synthesize roomJuzInfoDict;
    @synthesize delegate;
    
    - (id)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
    	if (self) {
        }
        return self;
    }
    
    -(void)drawPercentage:(CGPoint)p withRadius:(CGFloat)rad andPercentage:(int)perc inContext:(CGContextRef)cont{
    	UIGraphicsPushContext(cont);
    	CGContextBeginPath(cont);
    	CGContextMoveToPoint(cont, p.x, p.y);
    	CGContextAddLineToPoint(cont, p.x, (p.y-rad));
    	CGContextAddArc(cont, p.x, p.y, rad, START_ARC, START_ARC+(2*M_PI*perc/100), NO);
    	CGContextClosePath(cont);
    	[[UIColor colorWithRed:0 green:1 blue:0 alpha:0.4] setFill];
    	CGContextFillPath(cont);
    	UIGraphicsPopContext();
    }
    
    -(void)drawCircleAtPoint:(CGPoint)p withRadius:(CGFloat)rad andPercentage:(int)percent inContext:(CGContextRef)cont{
    	UIGraphicsPushContext(cont);
    	CGContextBeginPath(cont);
    	CGContextAddArc(cont, p.x, p.y, rad, 0, 2*M_PI, NO);
    	CGContextSetLineWidth(cont, 1.0);
    	[[UIColor colorWithRed:0.19 green:0.31 blue:0.31 alpha:0.65] setFill];
    	CGContextFillPath(cont);
    	UIGraphicsPopContext();
    	[self drawPercentage:p withRadius:rad andPercentage:percent inContext:(UIGraphicsGetCurrentContext())];
    }
    
    - (void)drawRect:(CGRect)rect {
    	CGPoint midPoint;
    	midPoint.x = self.bounds.origin.x + self.bounds.size.width/2;
    	midPoint.y = self.bounds.origin.y + self.bounds.size.height/2;
    	
    	CGFloat size = (self.bounds.size.height<self.bounds.size.width) ? self.bounds.size.height/2 : self.bounds.size.width/2;
    	size *= 0.15;
    		
    	CGContextRef context=UIGraphicsGetCurrentContext();
    	int tempPercent=25;
    	[self drawCircleAtPoint:midPoint withRadius:size andPercentage:tempPercent inContext:context];
    }
    
    - (void)dealloc {
        [super dealloc];
    }
    
    
    @end
    
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    You wouldn't add it to that code. UIScrollView is a container view. You'd need to add the scroll view as a subview to the parent view of your current circle view (seemingly) and then add your circle view as a subview to your scrollview. In other words:
    parentView -> scrollView -> circleView
     
  12. yygyt thread starter macrumors newbie

    Joined:
    Feb 4, 2011
    #12
    Thank you very much. This is what I was searching.
     

Share This Page