Saturday, 30 November 2013

Check whether NSString starts with aplhabet letter only.


+(BOOL)validateFirstCharacterAsAlphabet:(NSString *)str;
{
    NSRange first = [str rangeOfComposedCharacterSequenceAtIndex:0];
    NSRange match = [str rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet] options:0 range:first];
    if (match.location != NSNotFound) {
        // String starts with a letter
        return YES;
    }
    return NO;
    
}

Tuesday, 8 October 2013

Copy a UIView from xib and programmatically repeat it using NSKeyedArchiver and NSKeyedUnarchiver.


Lets create a _contactView in .h file using IBOutlet, and connect it in .xib file.
Connect it to the _contactView.

In .m:
//The following line will create a similar view as _contactView in the XIB;
 id copyOfView = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:_contactsView]];



// Repeat the view to generate multiple copies and add to the scroll view.
for (int i =0; i<_contactArray.count; i++) {
        UIView* tempView ;//= [[UIView alloc]init];
        id copyOfView = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:_contactsView]];
        tempView = copyOfView;
        tempView.tag = i;
        [tempView setFrame:CGRectMake(xOffset, yOffset, width, height)];
        yOffset += ySpacing+height;
        [_contactScrollView addSubview:tempView];

    }

    [_contactScrollView setContentSize:CGSizeMake(_contactsView.frame.size.width, _contactsView.frame.size.height*(_contactArray.count+1))];


Open other apps from iOS app using URL scheme. Open google maps, mail from the app, call a number, send sms using Xcode.


Use openURL: function of UIApplication.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];

Open The Google Maps:

// Create string for the place ...
NSString* placeStr = @"1 Infinite Loop, Cupertino, CA 95014";
// Encode URL encode
placeStr =  [addressText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// Now create the URL string ...
NSString* urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", placeStr];
// use openURL to launch the app ...
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];

Mail from the app:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://info@example.com"]];

Call a number:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://9876543210"]];

Send SMS:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:55555"]];

Open App Store:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/en/app/apple-store/id375380948?mt=8"]];

Wednesday, 18 September 2013

Vibrate UIView. Add Shivering effect to the view.


+ (void)vibrateView:(UIView*)view
{
    CABasicAnimation *shiverAnimationR;
    shiverAnimationR = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    shiverAnimationR.toValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(1)];
    //shiverAnimationR.toValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(-10)];
    shiverAnimationR.duration = 0.1;
    shiverAnimationR.repeatCount = 1000000.0; // Use A high Value
    shiverAnimationR.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

    [view.layer addAnimation: shiverAnimationR forKey:@"shiverAnimationR"];


    CABasicAnimation * shiverAnimationL;
    shiverAnimationL = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //shiverAnimationL 2.toValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(10)];
    shiverAnimationL.toValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(-1)];
    shiverAnimationL.duration = 0.1;
    shiverAnimationL.repeatCount = 1000000.0;
    shiverAnimationL.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

    [view.layer addAnimation: shiverAnimationL forKey:@"shiverAnimationL"];

}

Rotate the UIView clockwise. Rotate or swing UIView in circular manner.




+(void)rotateViewLikeCircle:(UIView*)view rotation:(int)numberOfRotation 
{
    CABasicAnimation *rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(180)];
    rotationAnimation.duration = 0.75;
    rotationAnimation.repeatCount = numberOfRotation;
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

Also don't forget to add the macros for converting degree to radian and visa-versa.

#define DEGREES_TO_RADIANS(degree) (M_PI * degree / 180.0)

#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))