Create screenshot in runtime (inside UIView)

by alex 7. November 2009 10:35

UIGraphicsBeginImageContext(self.bounds.size);

[self.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

Flip UIView

by alex 16. October 2009 02:15

-(IBAction)flipAction{

NSLog(@"flip");

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:1];

[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight

  forView:self.view cache:YES];

if ([discoView superview]){

[discoView removeFromSuperview];

[termsView addSubview:flipButton];

[self.view addSubview:termsView];

}

else

{

[termsView removeFromSuperview];

[self.view addSubview:discoView];

[discoView addSubview:flipButton];

}

[UIView commitAnimations];

}


Tags:

General

Move screen when keyboard popup

by alex 7. May 2009 14:18

//h file

 

@interface TestViewController : UIViewController<UITextFieldDelegate> {

    CGFloat animatedDistance;

}

 

//m file

 

 

 

- (void)textFieldDidBeginEditing:(UITextField *)textField{

    

    static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;

    static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;

    static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;

    static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;

    static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;

    

    CGRect textFieldRect =

    [self.view.window convertRect:textField.bounds fromView:textField];

    CGRect viewRect =

    [self.view.window convertRect:self.view.bounds fromView:self.view];

    

    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;

    CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;

    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;

    CGFloat heightFraction = numerator / denominator;

    if (heightFraction < 0.0)

    {

        heightFraction = 0.0;

    }

    else if (heightFraction > 1.0)

    {

        heightFraction = 1.0;

    }

    

    UIInterfaceOrientation orientation =

    [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIInterfaceOrientationPortrait ||

        orientation == UIInterfaceOrientationPortraitUpsideDown)

    {

        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);

    }

    else

    {

        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);

    }

    

    CGRect viewFrame = self.view.frame;

    viewFrame.origin.y -= animatedDistance;

    

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationBeginsFromCurrentState:YES];

    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    

    [self.view setFrame:viewFrame];

    

    [UIView commitAnimations];

    

}

 

 

- (void)textFieldDidEndEditing:(UITextField *)textField{

    

    static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;

    

    CGRect viewFrame = self.view.frame;

    viewFrame.origin.y += animatedDistance;

    

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationBeginsFromCurrentState:YES];

    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    

    [self.view setFrame:viewFrame];

    

    [UIView commitAnimations];

}

Clear UIView during drawing

by alex 14. March 2009 08:49

//Please note. To make view transparent you need to add:

//self.backgroundColor = [UIColor clearColor];

 

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextClearRect(context, self.bounds);


Tags:

General

Animate fade in/out

by nik 10. March 2009 03:24

 

IN *.m file  

------------------------------ 

#pragma mark ANIMATION

//-------fade in

-(void)animateFadingIn{

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:1.0];

[UIView setAnimationDelegate:self];

//set transformation

[UIView commitAnimations];

}

 

//-------fade out

- (void)animateFadingOut{

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:1.0];

[UIView setAnimationDidStopSelector:@selector(fadeAnimationDidStop:finished:context:)];

[UIView setAnimationDelegate:self];

//set transformation

[UIView commitAnimations];

}

- (void)fadeAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{

// push navigation controloller

}

 

 

IN *.h file

 

 -----------------------------

-(void)animateFadingIn;

-(void)animateFadingOut; 

 

 

////////////////////////////////////////////

//SAMPLE: 

logoBackdropView = [[UIImageView alloc] initWithFrame:CGRectMake(-36.0, 120.0, 479, 233)];

logoBackdropView.image = [UIImage imageNamed:@"logo_backdrop.png"];

[self.view addSubview:logoBackdropView]; 

 

 -(void)animateFadingIn{

 

  //state before animation

logoBackdropView.alpha = 0.0;

logoBackdropView.transform = CGAffineTransformMakeScale(2.002.00);

 

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:1.0];

[UIView setAnimationDelegate:self];

 

//state after animation

logoBackdropView.alpha = 1.0;

logoBackdropView.transform = CGAffineTransformMakeScale(1.00, 1.00);

[UIView commitAnimations];

}

 

 

Generate PDF on iPhone

by alex 13. February 2009 14:34

//Create empty PDF context on iPhone for later randering in it

-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path

{

    CGContextRef myOutContext = NULL;

    CFURLRef url;

    url = CFURLCreateWithFileSystemPath (NULL, // 1

path,

kCFURLPOSIXPathStyle,

false);

    if (url != NULL) {

        myOutContext = CGPDFContextCreateWithURL (url,// 2

  &inMediaBox,

  NULL);

        CFRelease(url);// 3

    }

    return myOutContext;// 4

}

 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"tmp.pdf"];

 

CGContextRef pdfContext = [self createPDFContext:scrolledView.bounds path:(CFStringRef)writableDBPath];

 

NSLog(@"PDF Context created");

CGContextBeginPage (pdfContext,nil); // 6

 

//turn PDF upsidedown

CGAffineTransform transform = CGAffineTransformIdentity;

transform = CGAffineTransformMakeTranslation(0, scrolledView.bounds.size.height);

transform = CGAffineTransformScale(transform, 1.0, -1.0);

CGContextConcatCTM(pdfContext, transform);

 

//Draw view into PDF

[scrolledView.layer renderInContext:pdfContext];

 

CGContextEndPage (pdfContext);// 8

CGContextRelease (pdfContext);

 

 

Tags: ,

General

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen