touch events problem - iphone

I found a very strange problem while handling touch events. The idea of an app is that i have an ImageView which contains a circle with text, that the user can rotate.
I've implemented a custom UIScrollView subclass to contain circle image. There i implemented methods touchesBegan, touchesMoved and touchesEnded in order to rotate my circle when user drags it left or right. Everything works fine, but when you try to drag it with one finger very fast from one side to another and in opposite direction, methods touchesBegan and touchesEnded are called different number of times. For example touchesBegan was called 1 time and touchesEnded 2 - 3 times. How can it be?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if([[event touchesForView:self] count]==1){
touchPoint = [[touches anyObject] locationInView:self];
previousTouchPoint = touchPoint;
angle = 0;
if (((touchPoint.x > 160)&&(touchPoint.y < 210))||((touchPoint.x < 160)&&(touchPoint.y > 210))) {
leftRotation = YES;
}
else {
leftRotation = NO;
}
currentMoveAngle = 0;
}
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{
if([[event touchesForView:self] count] == 1){
CGPoint newPoint = [[touches anyObject] locationInView:self];
CGPoint origin;
if (self.tag == 2) {
origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215);
}
else {
origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215);
}
previousTouchPoint.x -= origin.x;
previousTouchPoint.y -= origin.y;
CGPoint second = newPoint;
second.x -= origin.x;
second.y -= origin.y;
CGFloat rotationAngle = [self rotationFromFirstPoint:previousTouchPoint toSecondPoint:second];
previousTouchPoint = newPoint;
[self rotateContentToAngle:rotationAngle animated:NO];
currentMoveAngle += rotationAngle;
}
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{
if ([[event touchesForView:self] count] == 1){
rotating = YES;
CGFloat rotationAngle;
CGPoint origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215);
CGPoint lastPoint = [[touches anyObject] locationInView:self];
CGPoint touchP = touchPoint;
if ((touchP.x != lastPoint.x)||(touchP.y != lastPoint.y)) {
touchP.x -= origin.x;
touchP.y -= origin.y;
lastPoint.x -= origin.x;
lastPoint.y -= origin.y;
if (fabs(currentMoveAngle)>M_PI/6) {
NSInteger index = (int)trunc(currentMoveAngle/(M_PI/3));
currentMoveAngle-=(M_PI/3)*index;
NSLog(#"rotation index: %i",index);
}
if (leftRotation) {
rotationAngle = M_PI/3;
rotationAngle-=currentMoveAngle;
}
else {
rotationAngle = (-1)*M_PI/3;
rotationAngle-=currentMoveAngle;
}
[self rotateContentToAngle:rotationAngle animated:YES];
}
}
}

UIScrollView handle touch event very badly.... may be it is happening because of dragging of scrollView.
try this
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if (!self.dragging){
if([[event touchesForView:self] count]==1){
touchPoint = [[touches anyObject] locationInView:self];
previousTouchPoint = touchPoint;
angle = 0;
if (((touchPoint.x > 160)&&(touchPoint.y < 210))||((touchPoint.x < 160)&&(touchPoint.y > 210))) {
leftRotation = YES;
}
else {
leftRotation = NO;
}
currentMoveAngle = 0;
}
}
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{
if (!self.dragging){
if([[event touchesForView:self] count] == 1){
CGPoint newPoint = [[touches anyObject] locationInView:self];
CGPoint origin;
if (self.tag == 2) {
origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215);
}
else {
origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215);
}
previousTouchPoint.x -= origin.x;
previousTouchPoint.y -= origin.y;
CGPoint second = newPoint;
second.x -= origin.x;
second.y -= origin.y;
CGFloat rotationAngle = [self rotationFromFirstPoint:previousTouchPoint toSecondPoint:second];
previousTouchPoint = newPoint;
[self rotateContentToAngle:rotationAngle animated:NO];
currentMoveAngle += rotationAngle;
}
}
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{
if (!self.dragging){
if ([[event touchesForView:self] count] == 1){
rotating = YES;
CGFloat rotationAngle;
CGPoint origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215);
CGPoint lastPoint = [[touches anyObject] locationInView:self];
CGPoint touchP = touchPoint;
if ((touchP.x != lastPoint.x)||(touchP.y != lastPoint.y)) {
touchP.x -= origin.x;
touchP.y -= origin.y;
lastPoint.x -= origin.x;
lastPoint.y -= origin.y;
if (fabs(currentMoveAngle)>M_PI/6) {
NSInteger index = (int)trunc(currentMoveAngle/(M_PI/3));
currentMoveAngle-=(M_PI/3)*index;
NSLog(#"rotation index: %i",index);
}
if (leftRotation) {
rotationAngle = M_PI/3;
rotationAngle-=currentMoveAngle;
}
else {
rotationAngle = (-1)*M_PI/3;
rotationAngle-=currentMoveAngle;
}
[self rotateContentToAngle:rotationAngle animated:YES];
}
} }
}
or make scrollingEnabled property NO.

Related

How to rotate and point to cursor location in SpriteKit?

I am working with the SpriteKit's Spaceship Demo and I want it to rotate to the location of where I click on the screen and then move toward it.
The current code I have only makes it move up the screen starting at position 50,50:
sprite.position = CGPointMake(50,50);
SKAction *fly = [SKAction moveByX:0.0F y:50.0F duration:1];
[sprite runAction:[SKAction repeatActionForever:fly];
How would I make it work?
So long answer, this will help you.(I benefited from raywenderlich's book)
//Math Utilities
static inline CGPoint CGPointSubtract(const CGPoint a,
const CGPoint b)
{
return CGPointMake(a.x - b.x, a.y - b.y);
}
static inline CGPoint CGPointMultiplyScalar(const CGPoint a,
const CGFloat b)
{
return CGPointMake(a.x * b, a.y * b);
}
static inline CGFloat CGPointLength(const CGPoint a)
{
return sqrtf(a.x * a.x + a.y * a.y);
}
static inline CGPoint CGPointNormalize(const CGPoint a)
{
CGFloat length = CGPointLength(a);
return CGPointMake(a.x / length, a.y / length);
}
static inline CGPoint CGPointAdd(const CGPoint a,
const CGPoint b)
{
return CGPointMake(a.x + b.x, a.y + b.y);
}
static const float SHIP_SPEED = 60.0;
#implementation yourScene
{
SKSpriteNode *ship;
NSTimeInterval _lastUpdateTime;
NSTimeInterval _dt;
CGPoint _velocity;
CGPoint _lastTouchLocation;
}
-(void)didMoveToView:(SKView *)view
{
ship = [SKSpriteNode spriteNodeWithImageNamed:#"Spaceship"];
ship.position = CGPointMake(50, 50);
[self addChild:ship];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
[self moveShipToward:touchLocation];//Move Toward
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
{
if (_lastUpdateTime) {
_dt = currentTime - _lastUpdateTime;
} else {
_dt = 0;
}
_lastUpdateTime = currentTime;
CGPoint offset = CGPointSubtract(_lastTouchLocation, ship.position);
float distance = CGPointLength(offset);
if (distance < SHIP_SPEED * _dt) {
ship.position = _lastTouchLocation;
_velocity = CGPointZero;
} else {
[self moveSprite:ship velocity:_velocity];
[self rotateSprite:ship toFace:_velocity];
}
}
}
- (void)moveShipToward:(CGPoint)location
{
_lastTouchLocation = location;
CGPoint offset = CGPointSubtract(location, ship.position);
CGPoint direction = CGPointNormalize(offset);
_velocity = CGPointMultiplyScalar(direction, SHIP_SPEED);
}
- (void)moveSprite:(SKSpriteNode *)sprite
velocity:(CGPoint)velocity
{
CGPoint amountToMove = CGPointMultiplyScalar(velocity, _dt);
sprite.position = CGPointAdd(sprite.position, amountToMove);
}
- (void)rotateSprite:(SKSpriteNode *)sprite
toFace:(CGPoint)direction
{
sprite.zRotation = atan2f(direction.y, direction.x);
}

Dragging an Image View

I am trying to drag an image view. I have got little bit of success in doing so, but it is not behaving as i want. I wish that it should move only if touch inside the image and drag it.
But it is moving even if I am touching and dragging from anywhere on the screen.
I have written code like this:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//retrieve touch point
CGPoint pt= [[ touches anyObject] locationInView:[self.view.subviews objectAtIndex:0]];
startLocation = pt;
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint pt = [[touches anyObject] locationInView: [self.view.subviews objectAtIndex:0]];
CGRect frame = [[self.view.subviews objectAtIndex:0]frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[[self.view.subviews objectAtIndex:0] setFrame: frame];
}
The return value of locationInView method is point relative to the view frame. check it is or not in the view frame first.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect targetFrame = [self.view.subviews objectAtIndex:0].frame;
//retrieve touch point
CGPoint pt= [[ touches anyObject] locationInView:[self.view.subviews objectAtIndex:0]];
//check if the point in the view frame
if (pt.x < 0 || pt.x > targetFrame.size.width || pt.y < 0 || pt.y > targetFrame.size.height)
{
isInTargetFrame = NO;
}
else
{
isInTargetFrame = YES;
startLocation = pt;
}
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
if(!isInTargetFrame)
{
return;
}
//move your view here...
}
Try something like this:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//retrieve touch point
startLocation = [[ touches anyObject] locationInView:self.view];
// Now here check to make sure that start location is within the frame of
// your subview [self.view.subviews objectAtIndex:0]
// if it is you need to have a property like dragging = YES
// Then in touches ended you set dragging = NO
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint pt = [[touches anyObject] locationInView: [self.view.subviews objectAtIndex:0]];
CGRect frame = [[self.view.subviews objectAtIndex:0]frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[[self.view.subviews objectAtIndex:0] setFrame: frame];

Level Selector - Cocos2d

I'm trying to add 10 levels and 1 per page, which is 10 pages. How can I use this code to do that? Right now it only has two pages. Can anyone help?
-(id) init
{
if ((self = [super init]))
{
CGSize s = [[CCDirector sharedDirector] winSize];
self.isTouchEnabled = YES;
isDragging = NO;
lastX = 0.0f;
xVel = 0.0f;
contentWidth = s.width * 10.0;
currentPage = 0;
// main scrolling layer - add as child to this page layer.
scrollLayer = [[[LevelScene alloc] init] autorelease];
scrollLayer.anchorPoint = ccp(0, 1);
scrollLayer.position = ccp(0, 0);
[self addChild:scrollLayer];
[self schedule:#selector(moveTick:) interval:0.02f];
}
return self;
}
- (void) moveTick: (ccTime)dt
{
float friction = 0.99f;
CGSize s = [[CCDirector sharedDirector] winSize];
if (!isDragging)
{
// inertia
xVel *= friction;
CGPoint pos = scrollLayer.position;
pos.x += xVel;
// to stop at bounds
pos.x = MAX(-s.width, pos.x);
pos.x = MIN(0, pos.x);
if (pos.x == -s.width)
{
xVel = 0;
currentPage = 1;
}
if (pos.x == 0)
{
xVel = 0;
currentPage = 0;
}
// snap to page by quickly moving to it: e.g.: xVel = 40
if (fabsf(xVel) < 10)
{
if (pos.x < -s.width/2.0)
{
xVel = -40;
}
else {
xVel = 40;
}
}
scrollLayer.position = pos;
}
else {
xVel = (scrollLayer.position.x - lastX)/2.0;
lastX = scrollLayer.position.x;
}
}
- (void) ccTouchesBegan: (NSSet *)touches withEvent: (UIEvent *)event
{
isDragging = YES;
}
- (void) ccTouchesMoved: (NSSet *)touches withEvent: (UIEvent *)event
{
CGSize s = [[CCDirector sharedDirector] winSize];
UITouch *touch = [touches anyObject];
// simple position update
CGPoint a = [[CCDirector sharedDirector] convertToGL:[touch previousLocationInView:touch.view]];
CGPoint b = [[CCDirector sharedDirector] convertToGL:[touch locationInView:touch.view]];
CGPoint nowPosition = scrollLayer.position;
nowPosition.x += (b.x - a.x);
nowPosition.x = MAX(-s.width, nowPosition.x);
nowPosition.x = MIN(0, nowPosition.x);
scrollLayer.position = nowPosition;
}
- (void) ccTouchesEnded: (NSSet *)touches withEvent: (UIEvent *)event
{
isDragging = NO;
}
Any help is greatly appreciated! Thanks!
Jon, it seems like you're trying to recreate a UIScrollView in cocos2d. If that's the case, might I suggest using an existing open source project known as CCScrollLayer (HERE) Out of the box it should do everything you need to do and is pretty easy to extend to meet your needs better.

Implement Swipe and Tap together in a UIView

I want my UIImageView to detect both Tap and Swipe gesture. I've coded the touchesBegan method for Swipe detection, I just want to knw how can i make my ImageView detect both the gestures.
how to code the touchesBegan so that it can handle both of them??
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] != 1)
return;
floatSwipeStartX = [[touches anyObject] locationInView:self.view].x;
floatSwipeStartY = [[touches anyObject] locationInView:self.view].y;
intSwipeDirection = 0;
isSwiping = YES;
viewUp.hidden = NO;
viewLeft.hidden = NO;
viewCurrent.hidden = NO;
viewRight.hidden = NO;
viewDown.hidden = NO;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (!isSwiping || [touches count] != 1){
return;
}
CGFloat swipeDistanceX = [[touches anyObject] locationInView:self.view].x - floatSwipeStartX;
CGFloat swipeDistanceY = [[touches anyObject] locationInView:self.view].y - floatSwipeStartY;
CGSize contentSize = [self setContentSize];
if (! intSwipeDirection) {
if (abs(swipeDistanceX) > abs(swipeDistanceY)) { // swipe left or right
intSwipeDirection = SWIPE_DIRECTION_HORIZONTAL;
} else {
intSwipeDirection = SWIPE_DIRECTION_VERTICAL;
}
}
if (intSwipeDirection == SWIPE_DIRECTION_HORIZONTAL) {
viewLeft.frame = CGRectMake(swipeDistanceX - contentSize.width, 0.0f, contentSize.width, contentSize.height);
viewCurrent.frame = CGRectMake(swipeDistanceX, 0.0f, contentSize.width, contentSize.height);
viewRight.frame = CGRectMake(swipeDistanceX + contentSize.width, 0.0f, contentSize.width, contentSize.height);
} else {
viewUp.frame = CGRectMake(0.0f, swipeDistanceY - contentSize.height, contentSize.width, contentSize.height);
viewCurrent.frame = CGRectMake(0.0f, swipeDistanceY, contentSize.width, contentSize.height);
viewDown.frame = CGRectMake(0.0f, swipeDistanceY + contentSize.height, contentSize.width, contentSize.height);
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGFloat swipeDistanceX = [[touches anyObject] locationInView:self.view].x - floatSwipeStartX;
CGFloat swipeDistanceY = [[touches anyObject] locationInView:self.view].y - floatSwipeStartY;
if (! isSwiping || (swipeDistanceX == 0 && swipeDistanceY == 0)) {
//[self updateViews];
return;
}
if (intSwipeDirection == SWIPE_DIRECTION_HORIZONTAL) {
if (swipeDistanceX > 50.0f) {
intNewDirection = SWIPE_TO_THE_RIGHT;
intCurrentView = intCurrentLeft;
} else if (swipeDistanceX < -50.0f) {
intNewDirection = SWIPE_TO_THE_LEFT;
intCurrentView = intCurrentRight;
}
} else { // vertical
if (swipeDistanceY > 50.0f) {
intNewDirection = SWIPE_TO_THE_UP;
intCurrentView = intCurrentUp;
} else if (swipeDistanceY < -50.0f) {
intNewDirection = SWIPE_TO_THE_DOWN;
intCurrentView = intCurrentDown;
}
}
[self updateViews];
isSwiping = NO;
}
Please guide me where to insert the code for Tap gesture. I'm using simulator 3.2
If you're using sdk 3.2 or higher, this is dead easy with the UIGestureRecognizer class.
Otherwise, I'd do something along the lines of checking the amount of movement of the finger between touches began and touches ended, calling the gesture a tap if the movement is minimal and a swipe if it's large.

Multi touch is not working perfectly on UIImageView

I am doing multi touch on UImageView means zoom in and zoom out on image view. I am using followng code but it doesn't work very well. Can anyone look at this code,
#import "ZoomingImageView.h"
#implementation ZoomingImageView
#synthesize zoomed;
#synthesize moved;
define HORIZ_SWIPE_DRAG_MIN 24
define VERT_SWIPE_DRAG_MAX 24
define TAP_MIN_DRAG 10
CGPoint startTouchPosition;
CGFloat initialDistance;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
moved = NO;
zoomed = NO;
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
}
- (void)dealloc {
if([timer isValid])
[timer invalidate];
[super dealloc];
}
- (void) setImage: (UIImage*)img
{
zoomed = NO;
moved = NO;
self.transform = CGAffineTransformIdentity;
[super setImage:img];
}
- (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint {
float x = toPoint.x - fromPoint.x;
float y = toPoint.y - fromPoint.y;
return sqrt(x * x + y * y);
}
- (CGFloat)scaleAmount: (CGFloat)delta {
CGFloat pix = sqrt(self.frame.size.width * self.frame.size.height);
CGFloat scale = 1.0 + (delta / pix);
return scale;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if([timer isValid])
[timer invalidate];
moved = NO;
switch ([touches count]) {
case 1:
{
// single touch
UITouch * touch = [touches anyObject];
startTouchPosition = [touch locationInView:self];
initialDistance = -1;
break;
}
default:
{
// multi touch
UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
initialDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:self]
toPoint:[touch2 locationInView:self]];
break;
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
if([timer isValid])
[timer invalidate];
/*if ([touches count] == 1) {
CGPoint pos = [touch1 locationInView:self];
self.transform = CGAffineTransformTranslate(self.transform, pos.x - startTouchPosition.x, pos.y - startTouchPosition.y);
moved = YES;
return;
}****/
if ((initialDistance > 0) && ([touches count] > 1)) {
UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
CGFloat currentDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:self]
toPoint:[touch2 locationInView:self]];
CGFloat movement = currentDistance - initialDistance;
NSLog(#"Touch moved: %f", movement);
CGFloat scale = [self scaleAmount: movement];
self.transform = CGAffineTransformScale(self.transform, scale, scale);
// }
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
if ([touches count] == 1) {
// double tap to reset to default size
if ([touch1 tapCount] > 1) {
if (zoomed) {
self.transform = CGAffineTransformIdentity;
moved = NO;
zoomed = NO;
}
return;
}
}
else {
// multi-touch
UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
CGFloat finalDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:self]
toPoint:[touch2 locationInView:self]];
CGFloat movement = finalDistance - initialDistance;
NSLog(#"Final Distance: %f, movement=%f",finalDistance,movement);
if (movement != 0) {
CGFloat scale = [self scaleAmount: movement];
self.transform = CGAffineTransformScale(self.transform, scale, scale);
NSLog(#"Scaling: %f", scale);
zoomed = YES;
}
}
}
- (void)singleTap: (NSTimer*)theTimer {
// must override
}
- (void)animateSwipe: (int) direction {
// must override
}
It is not working fine on device. can anyone tell that where i am wrong.
When you use any CGAffinTransform.... the value of the frame property becomes undefined. In your code you are using the frame.size.width and frame.size.height to calculate the change in size. After the first iteration of CGAffinTransformScale you would not get the right scale factor. According to the documentation bounds would be the right property for scale calculations.