Wednesday, October 31, 2012

Touch Began for particular CGRect and Rotating Image with finger moved


// sample code inside Touchesbegan method
CGRect tempRect = CGRectMake(x, y, width, height);
if (CGRectContainsPoint(tempRect, touchlocation)) 
{
    // do some thing
}
- (void)viewWillAppear:(BOOL)animated
{
    imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mycard_back_dark_bg.png"]];
   imageView2.frame=self.imageView.frame;
    
    imageView2.bounds = self.imageView.bounds;
    [self.view addSubview:self.imageView2];
    [self.view sendSubviewToBack:imageView2];
    
    UIImage *image = [UIImage imageNamed:@"mycard_back_dark_bg.png"];
    
    UIImageOrientation flippedOrientation = UIImageOrientationUpMirrored;
    
    
    
    switch (image.imageOrientation)
    {
        case UIImageOrientationUp:
                                   break;
            
        case UIImageOrientationDown:
                                    flippedOrientation = UIImageOrientationDownMirrored;
                                    break;
        case UIImageOrientationLeft:    
                                    break;
        case UIImageOrientationRight:
                                    break;
        case UIImageOrientationUpMirrored:
                                    break;
        case UIImageOrientationDownMirrored:
                                    break;
        case UIImageOrientationLeftMirrored:
                                    break;
        case UIImageOrientationRightMirrored:
                                    break;
            
           
    }
    UIImage * flippedImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:flippedOrientation];
    
    self.imageView2.image=flippedImage;
    
    UILabel *cardNo= [[UILabel alloc] initWithFrame:CGRectMake(self.imageView.frame.origin.x+5, self.imageView.frame.origin.y+10, self.imageView.frame.size.width-30, self.imageView.frame.size.height/4)];
    
    [cardNo setText:@"5081 5988 1045 2318"];
    [cardNo setTextColor:[UIColor whiteColor]];
    [cardNo setBackgroundColor:[UIColor clearColor]];
    
    [self.imageView addSubview:cardNo];
    
   
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
    
    
UITouch *touch = [touches anyObject];
CGPoint origin = [self.imageView center];
    
    // sample code inside Touchesbegan method
    CGRect tempRect = CGRectMake(0, 0, 320, 200);
    if (CGRectContainsPoint(tempRect, [touch locationInView:self.view]))
    {
        // do some thing
        CGFloat angle = angleBetweenLinesInRadians(origin, [touch previousLocationInView:self.view], origin, [touch locationInView:self.view]);
        
        // NSLog(@"angle %2f",angle);
        i=i+angle;
        
        // NSLog(@"value of i %f",i);
        self.imageView.layer.doubleSided=FALSE;
        
        //self.imageView2.hidden = YES;
        self.imageView.layer.transform=CATransform3DMakeRotation(i, 0.0, 0.1, 0.0);
        
        self.imageView2.layer.transform=CATransform3DMakeRotation(i, 0.0, 0.1, 0.0);
        
        //  NSLog(@"position.z %f",self.imageView.layer.zPosition);
        // NSLog(@"position.y %f",self.imageView.layer.position.y);
        
        CALayer* layer = [self.imageView.layer presentationLayer];
        float currentAngle = [[layer valueForKeyPath:@"transform.rotation.z"] floatValue];
        NSLog(@"currentAngle %f",currentAngle);
        
        CALayer* layer2 = [self.imageView2.layer presentationLayer];
        float currentAngle2 = [[layer2 valueForKeyPath:@"transform.rotation.z"] floatValue];
        NSLog(@"currentAngle2 %f",currentAngle2);
    }
    
    
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
    CALayer* layer = [self.imageView.layer presentationLayer];
    float currentAngle = [[layer valueForKeyPath:@"transform.rotation.z"] floatValue];
    NSLog(@"currentAngle %f",currentAngle);
    
    if (currentAngle==0) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.75];
        [UIView setAnimationDelay:0.0];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
         
         self.imageView.layer.transform=CATransform3DMakeRotation(0, 0.0, 0.1, 0.0);
         self.imageView2.layer.transform=CATransform3DMakeRotation(0, 0.0, 0.1, 0.0);
        
        [UIView commitAnimations];
        
        i=0;
        
    }else {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.75];
        [UIView setAnimationDelay:0.0];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        self.imageView.layer.transform=CATransform3DMakeRotation(3.14, 0.0, 0.1, 0.0);
        self.imageView2.layer.transform=CATransform3DMakeRotation(3.14, 0.0, 0.1, 0.0);
        
        [UIView commitAnimations];
        
       i=3.14;
    }
}
static inline CGFloat angleBetweenLinesInRadians(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPoint line2End) {
    
    CGFloat a = line1End.x - line1Start.x;
    CGFloat b = line1End.y - line1Start.y;
    CGFloat c = line2End.x - line2Start.x;
    CGFloat d = line2End.y - line2Start.y;
    
    CGFloat line1Slope = (line1End.y - line1Start.y) / (line1End.x - line1Start.x);
    CGFloat line2Slope = (line2End.y - line2Start.y) / (line2End.x - line2Start.x);
    
    CGFloat degs = acosf(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));
    
    
    return (line2Slope > line1Slope) ? degs : -degs;    
}