Apply Touches to Custom Control - ios

I made a Custom Circle Control, Were i am Sending values by Slider from my RootView Controller. Can You help me out, how to Apply touches to Circle Control were i can change the values, like making custom slider around my Custom Circle.
#import "RKCustomCircle.h"
#implementation RKCustomCircle
#synthesize sliderPerccentageValue;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
xPos = 320/2;
yPos = 250;
radius = 80;
rotationAngle = 0;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawCircleChart: context];
}
- (void) drawCircleChart:(CGContextRef) context
{
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, 320, 480));
float a = rotationAngle;
[self drawCirclewithStartingAngle:a withContext:context];
}
- (void) drawCirclewithStartingAngle:(float)startAngle withContext:(CGContextRef) context
{
float endAngle = startAngle + (sliderPerccentageValue/ 100) * (M_PI*2);
float adjY = yPos;
float rad = radius;
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
CGContextSetStrokeColorWithColor(context,[UIColor blackColor].CGColor);
CGContextSetLineWidth(context, 1.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, xPos, adjY);
CGContextAddArc(context, xPos, adjY, rad, startAngle, endAngle, 0);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
}
RootView is,
#implementation RKViewController
- (void)viewDidLoad
{
[super viewDidLoad];
circleView = [[RKCustomCircle alloc] initWithFrame:CGRectMake(0, 100, 320, 360)];
[circleView addTarget:self action:#selector(newValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:circleView];
}
- (IBAction)sliderValueChangedInPercentage:(UISlider *)sender {
circleView.sliderPerccentageValue =sender.value;
[circleView setNeedsDisplay];
}

Use UITapGestureRecognizer.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(oneTap:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[circleView addGestureRecognizer:singleTap];
Add this method
- (void)oneTap:(UIGestureRecognizer *)gesture
{
NSLog(#"Touch occur");
}

All your things are correct, but only the following part is incorrect.
- (IBAction)sliderValueChangedInPercentage:(UISlider *)sender {
circleView.sliderPerccentageValue =sender.value;
[circleView setNeedsDisplay];
}
The above code doesn't supply any value because you have attached to your control that implements UIControl, not UISlider. You don't have to either, your UIControl will work..
Instead you can have touch events inside your control itself no need to add any action from outside, and you have to detect circular touch and keep track of values, see this app here it will help you to get values when touched circular.
Hope it will help you. All the best.

You need to override the pointInside:withEvent: method of the circle view. Refer to this for more information.

Related

Detect (Using UITapGeusture) and Drag Line (Using UIPanGesture)

I want to drag a drawn line which is done by Me. The below code snippet working fine with help of stack overflow user answer.
//My instances in .h file
CGPoint location1,location2;
LineView* l;
- (void)viewDidLoad
{
[super viewDidLoad];
l = [[LineView alloc]initWithFrame:self.view.frame];
[self.view addSubview:l];
UIPinchGestureRecognizer *linepinch = [[UIPinchGestureRecognizer alloc]
initWithTarget:l action:#selector(handleLinePinch:)];
[l addGestureRecognizer:linepinch];
}
- (void)handleLinePinch:(UIPinchGestureRecognizer *)gesture
{
NSUInteger num_touches = [gesture numberOfTouches];
// save locations to some instance variables, like `CGPoint location1, location2;`
if (num_touches >= 1) {
location1 = [gesture locationOfTouch:0 inView:l];
}
if (num_touches >= 2) {
location2 = [gesture locationOfTouch:1 inView:l];
}
[l drawRect:location1 Loc2:location2];
[l setNeedsDisplay];
}
LineView.m
- (void)drawRect:(CGPoint)location1 Loc2:(CGPoint)location2 {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 5.0);
CGContextMoveToPoint(context, location1.x, location1.y);
CGContextAddLineToPoint(context, location2.x, location2.y);
CGContextStrokePath(context);
}
The above code snippet working fine but i want to drag this line by using UITapGeustureRecognizer or someother.thanks in advance.

Core Animation not running in View Controller that also contains a Sprite Kit scene

In my project, there’s a main View Controller with a subview that contains a Sprite Kit scene. The subview (“MySubview”) is implemented like this:
- (id) initWithFrame: (CGRect) frame {
self = [super initWithFrame: frame];
if (self) {
myScene* scene = [myScene sceneWithSize: CGSizeMake(WIDTH, HEIGHT)];
[self presentScene: scene];
}
return self;
}
The main View Controller initialises this subview like this:
- (void) viewDidLoad {
[super viewDidLoad];
_mySubview = [[MySubview alloc] initWithFrame: CGRectMake(X, Y, WIDTH, HEIGHT)];
[self.view addSubview: mySubview];
}
All this is good and working so far. But then I try adding Core Animation on an object outside of the Sprite Kit view. I add the following to my main View Controller’s implementation:
CAShapeLayer* circleLayer;
- (void) setupCircle {
CGPathRef circlePath = CGPathCreateWithEllipseInRect(CGRectMake(CIRCLE_X, CIRCLE_Y, CIRCLE_WIDTH, CIRCLE_HEIGHT), nil);
circleLayer = [CAShapeLayer layer];
circleLayer.path = circlePath;
circleLayer.fillColor = [UIColor orangeColor].CGColor;
circleLayer.strokeColor = [UIColor blueColor].CGColor;
circleLayer.lineWidth = 10;
[self.view.layer addSublayer: circleLayer];
}
- (void) animateCircle {
CABasicAnimation* circleAnimation = [CABasicAnimation animationWithKeyPath:#"strokeEnd"];
circleAnimation.duration = 4;
circleAnimation.fromValue = #0;
circleAnimation.toValue = #1;
[circleLayer addAnimation: circleAnimation
forKey: #"circleAnimation"];
}
And then I change my viewDidLoad method to:
- (void) viewDidLoad {
[super viewDidLoad];
_mySubview = [[MySubview alloc] initWithFrame: CGRectMake(X, Y, WIDTH, HEIGHT)];
[self.view addSubview: mySubview];
[self setupCircle];
[self animateCircle];
}
This draws the circle, but does not animate its stroke. When I comment out all the Sprite Kit subview code, it does animate. Is it a problem with threading, run loops, or something else? I’m stuck here. Thanks for the help.

Lines drawn using drawRect method not getting scrolled

I am creating an app where I want to draw lines on scrollView. I am able to draw lines.
Here is my code
#interface GraphOnScrollView : UIScrollView
#property(strong,nonatomic)NSMutableArray *intensityArray;
#end
import "GraphOnScrollView.h"
#implementation GraphOnScrollView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.userInteractionEnabled=YES;
self.scrollEnabled=YES;
UIButton *DirectMsgBtn1 =[UIButton buttonWithType:UIButtonTypeCustom];
DirectMsgBtn1.titleLabel.font =[UIFont systemFontOfSize:12.0];
[DirectMsgBtn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[DirectMsgBtn1 setTitle:#"direct message" forState:UIControlStateNormal];
[DirectMsgBtn1 setBackgroundColor:[UIColor clearColor]];
[DirectMsgBtn1 addTarget:self
action:#selector(DirectMessageViewPopUp:)
forControlEvents:UIControlEventTouchDown];
DirectMsgBtn1.frame = CGRectMake(0.0, 0, 100, 30);
[self addSubview:DirectMsgBtn1];
DirectMsgBtn1 = nil;
}
return self; }
// Only override drawRect: if you perform custom drawing. // An
empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
CGFloat y_Axix =20.0;
CGFloat lineWidth=1.0;
for (int i=0; i<[self.intensityArray count]; i++)
{
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, 2.0);
lineWidth+=1;
CGFloat red[4] = {1.0f, 0.5f, 0.0f, 1.0f};
CGContextSetStrokeColor(c, red);
CGContextBeginPath(c);
CGFloat width=[[self.intensityArray objectAtIndex:i] floatValue];
CGContextMoveToPoint(c, 5.0f, y_Axix);
CGContextAddLineToPoint(c, width, y_Axix);
CGContextStrokePath(c);
CGContextAddArc(c,width,y_Axix,1.0f,0,2*3.1415926535898,1);
CGContextDrawPath(c,kCGPathStroke);
y_Axix=y_Axix+50;
}
NSLog(#"intensity array %#", self.intensityArray);
self.contentSize = CGSizeMake(self.frame.size.width, 1000);
}
This is code I am using for adding scrollview on my view
GraphOnScrollView *GraphView =[[GraphOnScrollView
alloc]initWithFrame:CGRectMake(0.0, 150.0, 320.0, 280.0)];
GraphView.backgroundColor =[UIColor whiteColor];
GraphView.intensityArray =[NSMutableArray arrayWithObjects:#"60",#"100",#"40",#"10",#"20",#"40",#"40",#"100",
nil];
[self.view addSubview:GraphView];
Using this code the lines which i have drawn is not getting scrolled but the scrollview is scrolled. I don't know what is the problem.
Thanks
While drawing on any view it draws everything on the canvas of that view.
In the case of your, you are drawing on UIScrollView so all drawing performs on the canvas of the UIScrollView, so it is not scrollable.
To solve that problem you can create on separate UIView with the size you want, perform any drawing on it and add that view in scrollview. I think this will be the best solution.
You can also refer to this link for some more help.
Alternate Approach (i actually used uiview for drawing and made it look like scroll)
save the touch location in
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
record the touch point in touches moved.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}
now you can translate coordinate of your points as
newX=x+(touchstart.x-touchMoved.x);//do this for x of every point
newy=y+(touchstart.y-touchMoved.y);// do this for y of every point
put the above lines in a function and call them from touches moved you can do it in touches ended method as well if lag is ok with you.
Point is translate the point and calculate the degree of translation from user touch began and moved or swipe

Rotating rectangle around circumference of a circle (iOS)?

I am trying to rotate the rectangle around the circle. So far after putting together some code I found in various places (mainly here: https://stackoverflow.com/a/4657476/861181) , I am able to rotate rectangle around it's center axis.
How can I make it rotate around the circle?
Here is what I have:
OverlaySelectionView.h
#import <QuartzCore/QuartzCore.h>
#interface OverlaySelectionView : UIView {
#private
UIView* dragArea;
CGRect dragAreaBounds;
UIView* vectorArea;
UITouch *currentTouch;
CGPoint touchLocationpoint;
CGPoint PrevioustouchLocationpoint;
}
#property CGRect vectorBounds;
#end
OverlaySelectionView.m
#import "OverlaySelectionView.h"
#interface OverlaySelectionView()
#property (nonatomic, retain) UIView* vectorArea;
#end
#implementation OverlaySelectionView
#synthesize vectorArea, vectorBounds;
#synthesize delegate;
- (void) initialize {
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = NO;
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(rotateVector:)];
panRecognizer.maximumNumberOfTouches = 1;
[self addGestureRecognizer:panRecognizer];
}
- (id) initWithCoder: (NSCoder*) coder {
self = [super initWithCoder: coder];
if (self != nil) {
[self initialize];
}
return self;
}
- (id) initWithFrame: (CGRect) frame {
self = [super initWithFrame: frame];
if (self != nil) {
[self initialize];
}
return self;
}
- (void)drawRect:(CGRect)rect {
if (vectorBounds.origin.x){
UIView* area = [[UIView alloc] initWithFrame: vectorBounds];
area.backgroundColor = [UIColor grayColor];
area.opaque = YES;
area.userInteractionEnabled = NO;
vectorArea = area;
[self addSubview: vectorArea];
}
}
- (void)rotateVector: (UIPanGestureRecognizer *)panRecognizer{
if (touchLocationpoint.x){
PrevioustouchLocationpoint = touchLocationpoint;
}
if ([panRecognizer numberOfTouches] >= 1){
touchLocationpoint = [panRecognizer locationOfTouch:0 inView:self];
}
CGPoint origin;
origin.x=240;
origin.y=160;
CGPoint previousDifference = [self vectorFromPoint:origin toPoint:PrevioustouchLocationpoint];
CGAffineTransform newTransform =CGAffineTransformScale(vectorArea.transform, 1, 1);
CGFloat previousRotation = atan2(previousDifference.y, previousDifference.x);
CGPoint currentDifference = [self vectorFromPoint:origin toPoint:touchLocationpoint];
CGFloat currentRotation = atan2(currentDifference.y, currentDifference.x);
CGFloat newAngle = currentRotation- previousRotation;
newTransform = CGAffineTransformRotate(newTransform, newAngle);
[self animateView:vectorArea toPosition:newTransform];
}
-(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint
{
CGPoint result;
CGFloat x = secondPoint.x-firstPoint.x;
CGFloat y = secondPoint.y-firstPoint.y;
result = CGPointMake(x, y);
return result;
}
-(void)animateView:(UIView *)theView toPosition:(CGAffineTransform) newTransform
{
[UIView setAnimationsEnabled:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.0750];
vectorArea.transform = newTransform;
[UIView commitAnimations];
}
#end
here is attempt to clarify. I am creating the rectangle from a coordinates on a map. Here is the function that creates that rectangle in the main view. Essentially it is the middle of the screen:
overlay is the view created with the above code.
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if (!circle){
circle = [MKCircle circleWithCenterCoordinate: userLocation.coordinate radius:100];
[mainMapView addOverlay:circle];
CGPoint centerPoint = [mapView convertCoordinate:userLocation.coordinate toPointToView:self.view];
CGPoint upPoint = CGPointMake(centerPoint.x, centerPoint.y - 100);
overlay = [[OverlaySelectionView alloc] initWithFrame: self.view.frame];
overlay.vectorBounds = CGRectMake(upPoint.x, upPoint.y, 30, 100);
[self.view addSubview: overlay];
}
}
Here is the sketch of what I am trying to achieve:
Introduction
A rotation is always done around (0,0).
What you already know:
To rotate around the center of the rectangle you translate the rect to origin, rotate and translate back.
Now for your question:
to rotate around a center point of a circle, simply move the center of the rectangle such that the circle is at (0,0) then rotate, and move back.
start positioning the rectangle at 12 o clock, with the center line at 12.
1) as explained you always rotate around 0,0, so move the center of the circle to 0,0
CGAffineTransform trans1 = CGAffineTransformTranslation(-circ.x, -circ.y);
2) rotate by angle
CGAffineTransform transRot = CGAffineTransformRotation(angle); // or -angle try out.
3) Move back
CGAffineTransform transBack = CGAffineTransformTranslation(circ.x, circ.y);
Concat these 3 rotation matrices to one combibed matrix, and apply it to the rectangle.
CGAffineTransformation tCombo = CGAffineTransformConcat(trans1, transRot);
tCombo = CGTransformationConcat(tCombo, transback);
Apply
rectangle.transform = tCombo;
You probably should also read the chapter about Transformation matrices in Quartz docu.
This code is written with a text editor only, so expect slighly different function names.

How to change uiview color when user touches the screen

I am creating an ipad app and I have a button that can create a rectangle with red fill and blue border. I want to change the border color when i touch the created rectangle. which statement should i use? Thanks
yourRect and borderColor are iVars. You have to write method changeBorderColor for logic of changing color ever time the rectangle is touched.
- (void) init
{
yourRect = CGRectMake(100,100, 200,200);
borderColor = [UIColor blueColor];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
[self addGestureRecognizer:singleTap:];
[singleTap release];
}
-(void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer
{
CGPoint tapLocation = [gestureRecognizer locationInView:shareView];
if(CGRectContainsPoint(yourRect, tapLocation))
{
borderColor = [self changeBorderColor]; //Change color
}
}
- (void)drawRect:(CGRect)rect;
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillRect(context, yourRect);
CGContextSetStrokeColorWithColor(context, borderColor.CGColor);
CGContextStrokeRect(context, yourRect);
}

Resources