Tuesday, September 21, 2010

Simple animation of snowFlake

// put the flake in our main view

[self.view addSubview:flakeView];

[UIView beginAnimations:nil context:flakeView];
 
// set up how fast the flake will fall
 
[UIView setAnimationDuration:4 * speed];
 
// set the postion where flake will move to
 

flakeView.frame = CGRectMake(endX, 500.0,16, 16);
 
// set a stop callback so we can cleanup the flake when it reaches the
// end of its animation
 
[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
 
[UIView setAnimationDelegate:self];
 
[UIView commitAnimations];

Resize image and keep aspect ratio

 -(UIImage *)resizeImage:(UIImage *)image {


   int w = image.size.width;   
   int h = image.size.height; 
   CGImageRef imageRef = [image CGImage]; 
   int width, height; 
   int destWidth = 640;
   int destHeight = 480; 
if(w > h){
        width = destWidth;  height = h*destWidth/w; 
   }
else
    {
     height = destHeight;
     width = w*destHeight/h;
        } 
  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
  CGContextRef bitmap;  bitmap = CGBitmapContextCreate(NULL, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedFirst);


if (image.imageOrientation == UIImageOrientationLeft)
   {
       CGContextRotateCTM (bitmap, M_PI/2); 
       CGContextTranslateCTM (bitmap, 0, -height);
      }
 else if (image.imageOrientation == UIImageOrientationRight) { 

      CGContextRotateCTM (bitmap, -M_PI/2);
     CGContextTranslateCTM (bitmap, -width, 0);
   }
else if (image.imageOrientation == UIImageOrientationUp) {


}
  else if (image.imageOrientation == UIImageOrientationDown) {

     CGContextTranslateCTM (bitmap, width,height);
    CGContextRotateCTM (bitmap, -M_PI); 
  }
    CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef); 
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
     UIImage *result = [UIImage imageWithCGImage:ref];
    CGContextRelease(bitmap);  CGImageRelease(ref);  return result;
}

gesture

GestureUIDelegate.h


@protocol GestureUIDelegate

-(void) singleTapGesture:(UITouch*)touch;
-(void) doubleTapGesture:(UITouch*)touch;
-(void) twoFingerTouchGestureAt:(UITouch*)touch1 and:(UITouch*)touch2;
-(void) touchAndHoldGesture:(UITouch*)touch;

@end





UIGestureView.h


#import "GestureUIDelegate.h"

@interface UIBase : UIView
{
id gestureDelegate; //bug with blogger. it won't show that id implements the GestureUIDelegate protocol

BOOL movable;
BOOL resizable;

BOOL userMoving;
BOOL userResizing;
CGPoint lastPoint;

int resizeHandleSize;

double touchDelay;
double tapDelay;

@private
UIImageView* border;

NSTimer* touchTimer;
UITouch* heldTouch;

float minWidth;
float minHeight;


NSTimer* tapTimer;
UITouch* tapTouch;
}

#pragma mark Properties
@property (nonatomic, retain) id gestureDelegate;

@property BOOL movable;
@property BOOL resizable;

@property float minWidth;
@property float minHeight;
@property float x;
@property float y;
@property float width;
@property float height;

#pragma mark Public Methods
-(void) initializeDefaults;

#pragma mark Custom Events
-(void) singleTapGesture:(UITouch*)touch;
-(void) doubleTapGesture:(UITouch*)touch;
-(void) twoFingerTouchGestureAt:(UITouch*)touch1 and:(UITouch*)touch2;
-(void) touchAndHoldGesture:(UITouch*)touch;

#pragma mark Private Methods

-(BOOL) touchInWindow:(UITouch*) touch;
-(BOOL) touchInBottomRightCorner:(UITouch*) touch;

-(void) startTouchAndHold:(UITouch*)touch;
-(void) cancelTouchAndHold;

-(void) startSingleTouch:(UITouch*)touch;
-(void) cancelSingleTouch;

@end



UIGestureView.m




#import "UIGestureView.h"

@implementation UIBase

@synthesize resizable;
@synthesize movable;
@synthesize gestureDelegate;

@dynamic x;
@dynamic y;
@dynamic width;
@dynamic height;
@dynamic minWidth;
@dynamic minHeight;

-(id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(!self)
return nil;
[self initializeDefaults];

return self;
}

-(void) initializeDefaults
{
self.minWidth = 50;
self.minHeight = 50;
resizeHandleSize = 20;
self.backgroundColor = [UIColor redColor];
self.multipleTouchEnabled = YES;
touchDelay = 0.5;
tapDelay = 0.2;
//self.resizable = YES;
//self.movable = YES;
}


#pragma mark Touch Events

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self superview] bringSubviewToFront:self];
UITouch* touch = [touches anyObject];

if(touch.tapCount == 2)
{
[self cancelSingleTouch];
[self doubleTapGesture:touch];
}
else if(touch.tapCount == 1)
{
if([touches count] == 1)
{
lastPoint = [touch locationInView:self.superview];
BOOL touchInWindow = [self touchInWindow:touch];
if(touchInWindow)
{
userResizing = [self touchInBottomRightCorner:touch] && resizable;
if(!userResizing)
{
[self startTouchAndHold:touch];
userMoving = touchInWindow && movable;
}
}
}
}
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self cancelTouchAndHold];
UITouch* touch = [touches anyObject];
CGPoint point = [touch locationInView:self.superview];
float xChange = point.x - lastPoint.x;
float yChange = point.y - lastPoint.y;
if(userResizing)
{
float width = self.frame.size.width + xChange;
float height = self.frame.size.height + yChange;
if (width >= self.minWidth && height >= self.minHeight) 
{
self.width = width;
self.height = height;
[self setNeedsDisplay];
}
}
else if(userMoving)
{
float oldX = self.frame.origin.x;
float oldY = self.frame.origin.y;
float x = oldX + xChange;
float y = oldY + yChange;
self.x = x;
self.y = y;
}
lastPoint = point;
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self cancelTouchAndHold];
if([touches count] >= 2)
{
UITouch* touch1 = [[touches allObjects] objectAtIndex:0];
UITouch* touch2 = [[touches allObjects] objectAtIndex:1];
[self twoFingerTouchGestureAt:touch1 and:touch2];
}
else
{
UITouch* touch = [touches anyObject];
if([touch tapCount]== 1)
[self startSingleTouch:touch];
}
userResizing = NO;
userMoving = NO;
}


#pragma mark Private Methods

-(void) singleTapGestureSucceeded
{
UITouch* touch = tapTouch;
[self cancelSingleTouch];
[self singleTapGesture:touch];
}

-(void) startSingleTouch:(UITouch*)touch
{
tapTouch = touch;
tapTimer = [[NSTimer scheduledTimerWithTimeInterval:tapDelay target:self selector:@selector(singleTapGestureSucceeded) userInfo:nil repeats:NO] retain];
}

-(void) cancelSingleTouch
{
if(tapTimer)
{
[tapTimer invalidate];
[tapTimer release];
}
tapTouch = nil;
tapTimer = nil;
}

-(void) startTouchAndHold:(UITouch*)touch
{
heldTouch = touch;
touchTimer = [[NSTimer scheduledTimerWithTimeInterval:touchDelay target:self selector:@selector(touchHasBeenHeld) userInfo:nil repeats:NO] retain];
}

-(void) cancelTouchAndHold
{
if(touchTimer)
{
[touchTimer invalidate];
[touchTimer release];
}

touchTimer = nil;
heldTouch = nil;
}

-(void) touchHasBeenHeld
{
userMoving = NO;
UITouch* touch = heldTouch;
[self cancelTouchAndHold];
[self touchAndHoldGesture:touch];
}

-(BOOL) touchInWindow:(UITouch*) touch
{
CGPoint location = [touch locationInView:self];
int x = location.x;
int y = location.y;
int width = self.frame.size.width;
int height = self.frame.size.height;
return (x > 0 && x < width) && (y > 0 && y < height);
}

-(BOOL) touchInBottomRightCorner:(UITouch*) touch
{
CGPoint location = [touch locationInView:self];
int x = location.x;
int y = location.y;
int width = self.frame.size.width;
int height = self.frame.size.height;
return (x > width - resizeHandleSize && x < width) && (y > height - resizeHandleSize && y < height);
}

#pragma mark Gestures
-(void) singleTapGesture:(UITouch*)touch
{
[gestureDelegate singleTapGesture:touch];
}
-(void) doubleTapGesture:(UITouch*)touch 
{
[gestureDelegate doubleTapGesture:touch];
}
-(void) twoFingerTouchGestureAt:(UITouch*)touch1 and:(UITouch*)touch2
{
[gestureDelegate twoFingerTouchGestureAt:touch1 and:touch2];
}
-(void) touchAndHoldGesture:(UITouch*)touch
{
[gestureDelegate touchAndHoldGesture:touch];
}

#pragma mark Properties

-(float) x
{
return self.frame.origin.x;
}

-(void) setX:(float) x
{
CGRect newFrame = CGRectMake(x, self.y, self.width, self.height);
self.frame = newFrame;
}

-(float) y
{
return self.frame.origin.y;
}

-(void) setY:(float) y
{
CGRect newFrame = CGRectMake(self.x, y, self.width, self.height);
self.frame = newFrame;
}

-(float) width
{
return self.frame.size.width;
}

-(void) setWidth:(float) width
{
CGRect newFrame = CGRectMake(self.x, self.y, width, self.height);
self.frame = newFrame;
}

-(float) height
{
return self.frame.size.height;
}

-(void) setHeight:(float) height
{
CGRect newFrame = CGRectMake(self.x, self.y, self.width, height);
self.frame = newFrame;
}

-(float) minWidth
{
return minWidth;
}

-(void) setMinWidth:(float)width
{
minWidth = width;
}

-(float) minHeight
{
return minHeight;
}

-(void) setMinHeight:(float)height
{
minHeight = height;
}

@end


How to fade out info button after some seconds ???

[button setAlpha: 1.0f];

[UIView beginAnimations: nil context:NULL];

// time delay in seconds to start

[UIView setAnimationDelay:2.0f];

// time in seconds for the fading, 0.25sec is the default apple time for small animations

[UIView setAnimationDuration:0.25f];

[UIView setAnimationBeginsFromCurrentState: YES];

[button setAlpha: 0.0f];

[UIView commitAnimations];

Simple Animation using a series of Images

NSArray *myImages = [NSArray arrayWithObjects:

[UIImage imageNamed:@"myImage1.png"],

[UIImage imageNamed:@"myImage2.png"],

[UIImage imageNamed:@"myImage3.png"],
[UIImage imageNamed:@"myImage4.gif"], nil]; // series of images that you want to animate

UIImageView *myAnimatedView = [UIImageView alloc];

[myAnimatedView initWithFrame:[self bounds]];

myAnimatedView.animationImages = myImages;

myAnimatedView.animationDuration = 0.25;     // time in seconds

 myAnimatedView.animationRepeatCount = 0;     // 0 = loops forever




[myAnimatedView startAnimating];

[self addSubview:myAnimatedView];

[myAnimatedView release];