Wednesday, July 31, 2013

Animate Mask in Objective C

First of all you can view my masking image logic here


Now If i click the screen and want to animate this mask, then we can apply the below logic

-(void)moveLayer:(CALayer*)layer to:(CGPoint)point
    // Prepare the animation from the current position to the new position
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.fromValue = [layer valueForKey:@"position"];
    animation.toValue = [NSValue valueWithCGPoint:point];
    animation.duration = .3;
    layer.position = point;
    [layer addAnimation:animation forKey:@"position"];

-(void)resizeLayer:(CALayer*)layer to:(CGSize)size
    CGRect oldBounds = layer.bounds;
    CGRect newBounds = oldBounds;
    newBounds.size = size;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
    animation.fromValue = [NSValue valueWithCGRect:oldBounds];
    animation.toValue = [NSValue valueWithCGRect:newBounds];
    animation.duration = .3;
    layer.bounds = newBounds;    
    [layer addAnimation:animation forKey:@"bounds"];

- (UIImageView*) animateMaskedImage:(UIImage *)image withMasked:(UIImage *)maskImage width:(int )wd andHeight:(int )ht andSize:(CGSize)size andPosition:(CGPoint)pt {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    CALayer *mask = [CALayer layer];
    mask.contents = (id)[maskImage CGImage];
    mask.frame = CGRectMake(0, 0, wd, ht);
    //Masking Animation
    [self resizeLayer:mask to:size];
    [self moveLayer:mask to:pt];
    imageView.layer.mask = mask;
    imageView.layer.masksToBounds = YES;
    return imageView;

Now we just need to call animatedMaskedImage function in our touch event like below

        [self.imageView1 removeFromSuperview];
        self.imageView1 = [self animateMaskedImage:self.image1 withMasked:self.mask1 width:1024 andHeight:768 andSize:CGSizeMake(1024*4, 768*4) andPosition:CGPointMake(1024*2, 768*2)];
        [self.completeSingleView addSubview:self.imageView1];

Tuesday, July 30, 2013

Masking Image with a transparent PNG

Hi i have made a function in which you can pass 2 UIImages and it will mask with the transparent Image

- (UIImageView*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    CALayer *mask1 = [CALayer layer];
    mask1.contents = (id)[maskImage CGImage];
    mask1.frame = CGRectMake(0, 0, 1024, 768);
    imageView.layer.mask = mask1;
    imageView.layer.masksToBounds = YES;
    return imageView;

Its returns the UIImageView, which you can add directly in UIView. You can call this method like following:

    UIImage *image = [UIImage imageNamed:@"screenFull.png"];
    UIImage *mask = [UIImage imageNamed:@"screen_mask.png"];
    [self.completeSingleView addSubview:[self maskImage:image withMask:mask]];

Here completeSingleView is a UIView.

Reference is taken from

Sunday, July 28, 2013

Removing some character in NSString in xCode

Suppose i don't want ",\': characters in my string then we can apply the below login to remove this from a NSString

NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"\",\\'"];
resultString = [[resultString componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];

NSLog(@"%@", resultString);

Split String in xCode

Suppose we have a String nums = "123,456,678,135,246,468,680"

Now if we want each number separated in an array then we can use the following method

NSArray *resultAr = [nums componentsSeparatedByString:@"|"]; 

Now we can track them by following

NSLog(@" %i %i %i", resultAr[0], resultAr[1], resultAr[2]);

Communicate with multiple storyboard in xcode

Suppose we have created a storyboard "GameScreen.storyboard" and linked that Storyboard to its ViewController named "GameScreenVC".

1. Now write this code on the events required.

    UIStoryboard *gameScreen = [UIStoryboard storyboardWithName:@"GameScreen" bundle:nil];
      UIViewController *gameScreenVC = [gameScreen instantiateInitialViewController];
gameScreenVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;//This is the Animation part

      [self presentViewController:gameScreenVC animated:YES completion:nil];

UIAlertView in xCode

Here i am showing you an Alert as in below Image

It has 2 buttons OK and Cancel. We can put any name in place of "OK" or "Cancel".

Ok... So here is the simple code for this.

1. First define a UIAlertView in top like

@property (nonatomic) UIAlertView *alert;

2. Add Alert like below 

self.alert = [[UIAlertView alloc]initWithTitle:@"Signup Alert"
                                       message:@"We will redirect to our site. where you can signup through your details. Press Ok to switch there or Canel to skip signup."
                             otherButtonTitles:@"Cancel", nil];
self.alert.delegate = self;
self.alert.tag=101;//add tag to alert

[self.alert show];

3. Now if you have declared delegate to self then write the below function, which will be called automatically.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    if(self.alert.tag ==101)     // check alert by tag
        if (buttonIndex == 0)
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @""]];
            //Skipping Signup

Simple WebService Connection with xCode

First of all create a response data as NSMutableData and class ".h" file

@property (nonatomic) NSMutableData *responseData;

Now Just do as follow in class ".m" file

-(void)validateUser:(NSString *)userId andPassword:(NSString *)userPassword
    NSString *envelopeText = [NSString stringWithFormat:
                              @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                              "<soap:Envelope xmlns:xsi=\"\"  xmlns:xsd=\"\"                    xmlns:soap=\"\">"
                              "<ValidateUser xmlns=\"\">"
                              "</soap:Envelope>", userId, userPassword];
    envelopeText = [NSString stringWithFormat:envelopeText, userId, userPassword];
    NSData *envelope = [envelopeText dataUsingEncoding:NSUTF8StringEncoding];
    // construct request
    NSString *url = @"";
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:envelope];
    [request setValue:@"text/xml;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    // Remember to put text/xml;charset=utf-8 in code, otherwise no result will be fetched
    [request setValue:[NSString stringWithFormat:@"%d", [envelope length]] forHTTPHeaderField:@"Content-Length"];
    // fire away
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (connection)
        self.responseData = [NSMutableData data];
        NSLog(@"NSURLConnection initWithRequest: Failed to return a connection.");

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    [self.responseData setLength:0]; }
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.responseData appendData:data];
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"connection didFailWithError: %@ %@", error.localizedDescription,
          [error.userInfo objectForKey:NSURLErrorFailingURLStringErrorKey]);

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"DONE. Received Bytes:%d",[self.responseData length]);
    NSString *theXml = [[NSString alloc] initWithBytes:[self.responseData mutableBytes] length:[self.responseData length] encoding:NSUTF8StringEncoding];
    NSLog(@"The final result :%@",theXml);//Here you will get the result xml in string