Drag Back to same position - iphone

Im using drag option in my application, Since i want to set the object in the same position, that is after drag back to same position, I tried with following coding it sets to som other postion, What change should i made in this,
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
if (touchPoint.x > self.img1.frame.origin.x &&
touchPoint.x < self.img1.frame.origin.x + self.img1.frame.size.width &&
touchPoint.y > self.img1.frame.origin.y &&
touchPoint.y < self.img1.frame.origin.y + self.img1.frame.size.height )
{
self.img1.backgroundColor = self.img1.backgroundColor;
}
self.img1.frame = CGRectMake(self.homePosition.x, self.homePosition.y,
self.img1.frame.size.width,
self.img1.frame.size.height);
}

In .h
CGPoint startPt;
In .m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
startPt = [[touches anyObject] locationInView:self.view];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
if (touchPoint.x > self.img1.frame.origin.x &&
touchPoint.x < self.img1.frame.origin.x + self.img1.frame.size.width &&
touchPoint.y > self.img1.frame.origin.y &&
touchPoint.y < self.img1.frame.origin.y + self.img1.frame.size.height )
{
self.img1.backgroundColor = self.img1.backgroundColor;
}
self.img1.frame = CGRectMake(startPt.x, startPt.y,
self.img1.frame.size.width,
self.img1.frame.size.height);
}

Related

How to implement swipe with UIGestureRecognizer subclass

How do you implement a swipe with an UIGestureRecognizer subclass?
(in case you're wondering why I'd like to do this instead of using UISwipeGestureRecognizer, it's because I want to add swipe recognition to Chameleon's UIKit port)
My first go at it (also on Github):
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
_beganLocation = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint movedLocation = [touch locationInView:self.view];
CGFloat distance = _distance(_beganLocation, movedLocation);
if (distance < SWIPE_MIN_DISTANCE) return;
CGFloat angle = _angle(_beganLocation, movedLocation);
int direction = -1;
if (angle > 270 - SWIPE_MAX_ANGLE && angle < 270 + SWIPE_MAX_ANGLE) {
direction = UISwipeGestureRecognizerDirectionUp;
}
if (angle > 180 - SWIPE_MAX_ANGLE && angle < 180 + SWIPE_MAX_ANGLE) {
direction = UISwipeGestureRecognizerDirectionLeft;
}
if (angle > 90 - SWIPE_MAX_ANGLE && angle < 90 + SWIPE_MAX_ANGLE) {
direction = UISwipeGestureRecognizerDirectionDown;
}
if ((angle > 360 - SWIPE_MAX_ANGLE && angle <= 360) || (angle >= 0 && angle <= SWIPE_MAX_ANGLE)) {
direction = UISwipeGestureRecognizerDirectionRight;
}
if (direction == -1) {
self.state = UIGestureRecognizerStateFailed;
} else {
self.state = self.direction == direction ? UIGestureRecognizerStateRecognized : UIGestureRecognizerStateFailed;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
self.state = UIGestureRecognizerStateFailed;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
self.state = UIGestureRecognizerStateFailed;
}
Auxiliary functions:
static CGFloat _distance(CGPoint point1,CGPoint point2)
{
CGFloat dx = point2.x - point1.x;
CGFloat dy = point2.y - point1.y;
return sqrt(dx*dx + dy*dy);
};
static CGFloat _angle(CGPoint start, CGPoint end)
{
CGPoint origin = CGPointMake(end.x - start.x, end.y - start.y); // get origin point to origin by subtracting end from start
CGFloat radians = atan2f(origin.y, origin.x); // get bearing in radians
CGFloat degrees = radians * (180.0 / M_PI); // convert to degrees
degrees = (degrees > 0.0 ? degrees : (360.0 + degrees)); // correct discontinuity
return degrees;
}
It should be noted that Chameleon's implementation of gesture recognizers is incomplete and contains logic most likely specific to Twitterrific. Our fork contains further modifications.

How to set minimum & maximum y value?

I am implementing a draggable UIView in my application. The codes I have worked fine but I would like to set a restricted area the UIView can be moved.
My codes:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == camview)
{
CGPoint location = [touch locationInView:self.view;
startX = camview.center.x;
startY= location.y - camview.center.y;
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == camview)
{
CGPoint location = [touch locationInView:self.view];
location.y =location.y - startY;
location.x = startX;
camview.center = location;
}
}
So how can I set the minimum and maximum y value that the UIView can be dragged?
Thanks!
If you are interested in making sure the view frame doesn't go over or under a certain y value you could do the following,
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGFloat minY, maxY; //The position in your self.view you are interested in
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == camview)
{
CGPoint location = [touch locationInView:self.view];
location.y = location.y - startY;
location.y = MIN(location.y,maxY); //Always <= maxY
location.y = MAX(location.y,minY); //Always >= minY
location.x = startX;
camview.center = location;
}
}

iPhone - Inertial dragging

I have an object that the user has to drag around the screen. This is a regular UIImageView. Currently, the image can be dragged around but when you release it, it stops where your finger lifted the image. What I want is to create momentum, so I can give an impulse to the image and it scrolls until it bounces the screen edge.
I don't want nothing very complex like adding physics libraries and stuff like that. I want to keep it as minimum as possible.
How do I do that? Can you guys point me to some tutorial or in the right direction?
thanks.
Well one way of doing this without using any physics library is this:
First in your class create 4 instance variables like this:
CFTimeInterval startTime;
CGPoint startPoint;
CGPoint oldPoint;
BOOL imageViewTouched;
Than in your - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method have this code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouched] anyObject];
// Test to see if the touched view is your image view. If not just return.
if (touch.view != <#yourImageView#>) {
return;
}
startPoint = [touch locationInView:self.view];
oldPoint = startPoint;
startTime = CACurrentMediaTime();
imageViewTouched = YES;
}
For - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event do this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// Fingers were moved on the screen but your image view was not touched in the beginning
if (!imageViewTouched) {
return;
}
UITouch *touch = [[event allTouches] anyObject];
CGPoint newPoint = [touch locationInView:self.view];
<#yourImageView#>.frame = CGRectOffset(<#yourImageView#>.frame, newPoint.x - oldPoint.x, newPoint.y - oldPoint.y);
oldPoint = newPoint;
}
Now for - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event try this:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// Fingers were removed from the screen but your image view was not touched in the beginning
if (!imageViewTouched) {
return;
}
imageViewTouched = NO;
UITouch *touch = [[event allTouches] anyObject];
CGPoint endPoint = [touch locationInView:self.view];
CFTimeInterval endTime = CACurrentMediaTime();
CFTimeInterval timeDifference = endTime - startTime;
// You may play with this value until you get your desired effect
CGFloat maxSpeed = 80;
CGFloat deltaX = 0;
CGFloat deltaY = 0;
if (timeDifference < 0.35) {
deltaX = (endPoint.x - startPoint.x) / (timeDifference * 10);
deltaY = (endPoint.y - startPoint.y) / (timeDifference * 10);
}
if (deltaX > maxSpeed) { deltaX = maxSpeed; }
else if (deltaX < -maxSpeed) { deltaX = -maxSpeed; }
else if (deltaX > -5 && deltaX < 5) { deltaX = 0; }
if (deltaY > maxSpeed) { deltaY = maxSpeed; }
else if (deltaY < -maxSpeed) { deltaY = -maxSpeed; }
else if (deltaY > -5 && deltaY < 5) { deltaY = 0; }
[UIView begginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
<#yourImageView#>.frame = CGRectOffset(<#yourImageView#>.frame, deltaX, deltaY);
[UIView commitAnimations];
}
Please let me know if this makes sense and/or works for you. Cheers!

Drag View from the point you touch it iPhone

I know how to drag a view or object:
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == ViewMain)
{
CGPoint location = [touch locationInView:self.view];
ViewMain.center = location;
}
}
but I would like to start dragging that view or image from the point where I touched it. for example if I drag (I highlighted the view and placed blue on the cursor):
the moment I start dragging if I drag the mouse 20 px to the right for example then I would like the view to also drag 20 px instead of:
maybe I have to change the center of the view to the point where I fist touch it. How could I do that?
In other words I would like to do something like when you drag the apps on the iPad:
float startX = 0;
float startY = 0;
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == ViewMain)
{
CGPoint location = [touch locationInView:self.view];
startX = location.x - ViewMain.center.x;
startY = ViewMain.center.y;
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if( [touch view] == ViewMain)
{
CGPoint location = [touch locationInView:self.view];
location.x =location.x - startX;
location.y = startY;
ViewMain.center = location;
}
}

Move a UIImageView in y axis

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);
}