resignFirstResponder, the easy way!

If you have an UIControl in edit mode… but you have no idea which one is it… and you need to resignFirstResponder, there is an easy way to do this. No, you don’t need to hook to notifications. And you don’t need to find the firstResponder. This can be solved with just one call.

What you need to do is…

[cc lang=”objc”][_tableView endEditing:YES][/cc]

Note that you could have anything else rather than a _tableView.
Easy. Right?

CoreAnimation: Bounce animation… like the Camera button

Have you seen the animation in which the lockscreen bounces… if you tap over the camera button?. Guess what!. That can be done, quite easily, with Core Animation.

We’ll implement this as an extension to UIView. So.. add a file named ‘UIView+CoreAnimation.h/m’, and paste the following code:

REF: Thanks to the Cocoanetics author for sharing this. I’ve tweaked his code just a little bit.

[cc lang=”objc”]
#import

+ (CAKeyframeAnimation*)dockBounceAnimationWithViewHeight:(CGFloat)viewHeight
{
NSUInteger const kNumFactors = 22;
CGFloat const kFactorsPerSec = 30.0f;
CGFloat const kFactorsMaxValue = 128.0f;
CGFloat factors[kNumFactors] = {0, 60, 83, 100, 114, 124, 128, 128, 124, 114, 100, 83, 60, 32, 0, 0, 18, 28, 32, 28, 18, 0};

NSMutableArray* transforms = [NSMutableArray array];

for(NSUInteger i = 0; i < kNumFactors; i++) { CGFloat positionOffset = factors[i] / kFactorsMaxValue * viewHeight; CATransform3D transform = CATransform3DMakeTranslation(0.0f, -positionOffset, 0.0f); [transforms addObject:[NSValue valueWithCATransform3D:transform]]; } CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; animation.repeatCount = 1; animation.duration = kNumFactors * 1.0f/kFactorsPerSec; animation.fillMode = kCAFillModeForwards; animation.values = transforms; animation.removedOnCompletion = YES; // final stage is equal to starting stage animation.autoreverses = NO; return animation; } - (void)jump { CGFloat midHeight = self.frame.size.height * 0.5f; CAKeyframeAnimation* animation = [[self class] dockBounceAnimationWithViewHeight:midHeight]; [self.layer addAnimation:animation forKey:@"bouncing"]; } [/cc]

OSX Lion: Refresh Launchpad Contents

I recently came across a problem. The contents of the launchpad, somehow, got corrupt. I was seeing files that shouldn’t be there. So… how did i fix it?

Open this folder:

[cc lang=”bash”]~/Library/Application Support/Dock[/cc]

(CMD + Shift + G… and paste that!). Once you’re right there… you’ll need to delete delete the “.db” file. Last step… relaunch the dock!

[cc lang=”bash”]killall Dock[/cc]

That’s it!

CoreGraphics: Drawing Dashed Lines

Suppose you wanna draw a dashed line all around a control. What should we do?. Well… simple. We need to invoke some CoreGraphics dark magic… it’s pretty self explanatory. The catch to it is that it’ll draw a dashed line. Play with it..!

[cc lang=”objc”]
static CGFloat const kDashedBorderWidth = (2.0f);
static CGFloat const kDashedPhase = (0.0f);
static CGFloat const kDashedLinesLength[] = {4.0f, 2.0f};
static size_t const kDashedCount = (2.0f);

– (void)drawRect:(CGRect)rect
{
[super drawRect:rect];

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, kDashedBorderWidth);
CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);

CGContextSetLineDash(context, kDashedPhase, kDashedLinesLength, kDashedCount) ;

CGContextAddRect(context, rect);
CGContextStrokePath(context);
}
[/cc]