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]

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]

Current Selector’s Name

So… suppose you wanna log the name of the ‘current’ method. You could hardcode the method name, right there… virtually everywhere… or you can do this:

[cc lang=”objc”]
NSLog(@”[ %@ ] did something”, NSStringFromSelector(_cmd));
[/cc]

It’s simple. Yet, its something i didn’t know… three days ago!.

Twitter iOS SDK

I recently had to integrate one of my apps with Twitter. Let me say you something. I HATE TWITTER guys. Why?. Because everything is soooo complicated. Although there is a direct integration between Twitter and iOS 5, they have made it really hard for developers.

Why?. If you need to post tweets, backend side, you need to ask for ‘Reverse Auth‘ permissions.. and it’s not something that can be done automatically. They have to personally approve this.

So… if you’re like me, stressed dealing with those guys, check this out: https://github.com/bengottlieb/Twitter-OAuth-iPhone.

What is that?. A nice iOS library, which implements OAuth authentication against twitter servers. I’m tuning it, just a little bit. There is a ‘PIN’ mechanism implemented right there, which i’m not particular fond of. But besides that, it’ll help you open a WebView as a modalViewController, and authenticate the user into twitter.

Not the best i’ve imagined.. but the problem is solved. My idea is to implement a hybrid. If the user has no credentials stored in iOS 5, then i’ll fall back to this framework. Makes sense.. right?.

Fixing Bootstrap Errors

I’ve been having this error… A LOT…:

“Couldn’t register com.yourcompany.yourapp with the bootstrap server. Error: unknown error code. This generally means that another instance of this process was already running or is hung in the debugger.”

The bad side of this is that.. ps aux will not show anything useful. Thankfully, Mike Ash has solved this… i’m pasting below his script:

[cc lang=”bash”]
launchctl list|grep UIKitApplication|awk ‘{print $3}’|xargs launchctl remove
[/cc]

Source: Mike Ash Blog

%d