Move a UIImageView in y axis - iphone

I am working on a app and i want to move a UIImage in the Y axis only, not in Xaxis, here's what i did till now
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
stretchPoint = [[touches anyObject]locationInView:self.view];
arrowImage.center = stretchPoint;
}
The above code is moving the arrowImage in both the axis,
stretchPoint is an instance of CGPoint, My app is in portrait mode, can anyone give me a basic idea to how to do this, as the arrowImage is a instance of UIImageView.

this should work fine:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.x = arrowImage.center.x;
arrowImage.center = currentPoint;
}

Subclass the UIImage and implement the following code
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:[self superview]];
CGPoint newCenter = CGPointMake(self.center.x, currentPoint.y);
self.center = newCenter;
}

I am not sure as have not tested,but you can implement something like this :
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y = 20;
}

Something like:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
stretchPoint = [[touches anyObject] locationInView:self.view];
arrowImage.frame = CGPointMake(arrowImage.origin.x,
stretchPoint.y,
arrowImage.size.width,
arrowImage.size.height);
}

Related

touchesbegan, touchesmoved, touchesended issue

For various reasons, I've moved these methods from a UIView subclass to my viewcontroller. And I finally got it working, except for one thing. Not only am I able to drag the UIImageviews I've programmatically created, but the actual view controllers view is draggable too. Creating this much undesired effect. I guess it's the fact that it's touches anyobject, and the background itself is an object. I'm just not sure how exclude the background. I would think that it would need the "UserInteraction enabled", but I guess not? I only want it to make UIImageViews draggable. Please forgive my noobness. I'm still learning.
I have all the imageviews i'd want "touchable" in an NSMutableDictionary called "letterDictionary". Would it be possible to only have touch apply to what's in the dictionary?
http://imgur.com/W08dI
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch *touch = [touches anyObject];
touchPoint = [touch locationInView:self.view];
movingLetter = [touch view];
CGPoint pointInside = [touch locationInView:[touch view]];
if ([movingLetter pointInside:pointInside withEvent:event]) touchedInside = YES;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (touchedInside) {
UITouch *touch = [touches anyObject];
CGPoint newPoint = [touch locationInView:self.view]; // get the new touch location
movingLetter.center = CGPointMake(movingLetter.center.x + newPoint.x - touchPoint.x, movingLetter.center.y + newPoint.y - touchPoint.y);
touchPoint = newPoint;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (touchedInside) {
UITouch *touch = [touches anyObject];
CGPoint newPoint = [touch locationInView:self.view];
movingLetter.center = CGPointMake(movingLetter.center.x + newPoint.x - touchPoint.x, movingLetter.center.y + newPoint.y - touchPoint.y);
if (CGRectIntersectsRect([movingLetter frame], [placeHolder frame]))
{
movingLetter.center = placeHolder.center;
}
}
touchedInside = NO;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
touchedInside = NO;
}
You have the view that was touched,
UITouch *touch = [touches anyObject];
touchPoint = [touch locationInView:self.view];
movingLetter = [touch view];
just test to see if it is the class you are looking for (e.g. a UIImageView) then return
UITouch *touch = [touches anyObject];
if (![[touch view] isKindOfClass:[UIImageView class]])
{
return;
}
In this touchesBegen Method :
-(void)touchesBegan:(NSSet* )touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
UITouch *touch = [touches anyObject];
_previousPoint1 = [touch previousLocationInView:self.main_uiview];
_previousPoint2 = [touch previousLocationInView:self.main_uiview];
_currentPoint = [touch locationInView:self.main_uiview];
[self touchesMoved:touches withEvent:event];
self.bezierPath = [UIBezierPath bezierPath];
[self.bezierPath moveToPoint:_currentPoint];
}
TouchesMove Method
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent* )event
{
UITouch *touch = [touches anyObject];
_previousPoint2 = _previousPoint1;
_previousPoint1 = [touch previousLocationInView:self.main_uiview];
_currentPoint = [touch locationInView:self.main_uiview];
lastPoint = _currentPoint;
[_bezierPath addLineToPoint:lastPoint];
// calculate mid point
CGPoint mid1 = midPoint4(_previousPoint1, _previousPoint2);
CGPoint mid2 = midPoint4(_currentPoint, _previousPoint1);
UIGraphicsBeginImageContextWithOptions(self.bg_imageview.frame.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,brush);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineJoin(context,kCGLineJoinRound);
[self.bg_imageview.image drawInRect:CGRectMake(0, 0, self.bg_imageview.frame.size.width, self.bg_imageview.frame.size.height)];
CGContextMoveToPoint(context, mid1.x, mid1.y);
// Use QuadCurve is the key
CGContextAddQuadCurveToPoint(context, _previousPoint1.x, _previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetStrokeColorWithColor(context,[UIColor blackColor].CGColor);
CGContextSetLineWidth(context, 3.0);
CGContextStrokePath(context);
self.bg_imageview.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();}

how to highlight the image on touch event in iphone

hi friend i created this method for image select it working proper but i am facing problem on image when i touch the image i cant see image click or not i want highlight the image when i touch on it how can i do this
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location= [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location))
{
// flag like
select=1;
}
else if(CGRectContainsPoint(secImage.frame, location))
{
select=2;
}
[mComment resignFirstResponder];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location)) {
if(select==1) {
var=1;
}}
else if(CGRectContainsPoint(secImage.frame, location)) {
if(select==2) {
vars=1;
}}
select=0;
}
One thing you can do for highlight the image when you touch on it. When you touch on image you should change the alpha of selected image in touch began method and reset the image alpha in touch ended method. So its look like button.
For Ex.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location= [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location))
{
// flag like
select=1;
firstImage.alpha = 0.5;
}
else if(CGRectContainsPoint(secImage.frame, location))
{
select=2;
secImage.alpha = 0.5;
}
[mComment resignFirstResponder];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(firstImage.frame, location)) {
if(select==1) {
firstImage.alpha = 1.0;
var=1;
}}
else if(CGRectContainsPoint(secImage.frame, location)) {
if(select==2) {
secImage.alpha = 1.0;
vars=1;
}}
select=0;
}

moving UITextField in UIView

i am using UITextField on my UIView and need to drag UITextField with my pointer on my UIView but its not working!! here is my code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[UIView beginAnimations:#"Move" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationBeginsFromCurrentState:YES];
UITouch *touch = [[event allTouches] anyObject];
mytextField.center = [touch locationInView:myView];
[UIView commitAnimations]; }
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
mytextField.center = [touch locationInView:myView];}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}
You wanna move to UITextField with touches then use this code -
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint pt = [[touches anyObject] locationInView:textFieldObj];
// textFieldObj is your UITextfield
startLocationOfText = pt;
//startLocationOfText is just CGPoint declared globaly
}
now touches moved method
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint pt = [[touches anyObject] previousLocationInView:textField2];
CGFloat dx = pt.x - startLocationOfText.x;
CGFloat dy = pt.y - startLocationOfText.y;
CGPoint newCenter = CGPointMake(textFieldObj.center.x + dx, textFieldObj.center.y + dy);
textFieldObj.center = newCenter;
}
You can comments the touchesBegan method but for clear move you should keep this method.
Thank you!
Why you have taken animation in touches began.I think just comment code of touches began and try it will work.

Rotation based on touch problem

I'm making a simple dial that rotates as you drag your finger across it. It rotates great, but it also rotates when i touch anywhere on the screen and drag my finger.
How can i restrict the first touches to be only inside my imageview object? or where am i going wrong?
this is my code of trouble:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UIImage *image1 = [UIImage imageNamed:#"nav#2x.png"];
wheelfrom = [[UIImageView alloc] initWithImage:image1];
wheelfrom.frame =CGRectMake(10, -130, 300, 300);
[self addSubview:wheelfrom];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch =[[[event allTouches] allObjects] lastObject];
firstLoc = [touch locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch =[[[event allTouches] allObjects] lastObject];
CGPoint curLoc = [touch locationInView:self];
float fromAngle = atan2( firstLoc.y-wheelfrom.center.y,
firstLoc.x-wheelfrom.center.x );
float toAngle = atan2( curLoc.y-wheelfrom.center.y,
curLoc.x-wheelfrom.center.x );
float newAngle = angle + (toAngle - fromAngle);
CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);
wheelfrom.transform = cgaRotate;
angle = newAngle;
}
Thanks for your help!
You try like this,
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(wheelfrom.frame, location))
{
//do your things
}
}
You can try by checking if the point of touch is within the frame of the image view.Do what you want only if its yes.
Inside -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event, check the firstLoc is within your range.

drag object vertically

i like to drag this object vertically instead of horizontally, which is doin it now:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (CGRectContainsPoint(myObject.frame, location)){
CGPoint xLocation = CGPointMake(location.x, myObject.center.y);
myObject.center = xLocation;
}
}
CGPoint yLocation = CGPointMake(myObject.center.x, location.y);
myObject.center = yLocation;