Showing posts with label free code. Show all posts
Showing posts with label free code. Show all posts

Tuesday, October 18, 2011

CGAffineTransform Additions

As you probably know, Apple provides a bunch of functionality for manipulating objects in 2D space using CGAffineTransform. Oddly, Apple doesn't provide you with a way to extract the scale, transform, and rotation information from a CGAffineTransform and they don't provide any shearing functionality at all.

Here are some additional inline functions that I use. This adds the ability to extract component values of the CGAffineTransform and also adds the ability to create and extract shear information.

As always, this code is free to use without restriction or limitation, but has no warranty whatsoever. If you fix a bug, feel free to let me know about the fix so I can incorporate the fix.

#ifndef __MCP_AFFINE_TRANSFORM_ADDITIONS__
#define __MCP_AFFINE_TRANSFORM_ADDITIONS__

#import <CoreGraphics/CoreGraphics.h>

#ifdef __cplusplus
extern "C" {
#endif

#define degreesToRadian(x) (M_PI * x / 180.0)
#define radiansToDegrees(x) (180.0 * x / M_PI)

static inline CGAffineTransform CGAffineTransformMakeShear(CGFloat shearX, CGFloat shearY)
{
return CGAffineTransformMake(1.f, shearY, shearX, 1.f, 0.f, 0.f);
}

static inline CGAffineTransform CGAffineTransformShear(CGAffineTransform transform, CGFloat shearX, CGFloat shearY)
{
CGAffineTransform sheared = CGAffineTransformMakeShear(shearX, shearY);
return CGAffineTransformConcat(transform, sheared);
}

static inline CGFloat CGAffineTransformGetDeltaX(CGAffineTransform transform) {return transform.tx;};
static inline CGFloat CGAffineTransformGetDeltaY(CGAffineTransform transform) {return transform.ty;};
static inline CGFloat CGAffineTransformGetScaleX(CGAffineTransform transform) {return sqrtf( (transform.a * transform.a) + (transform.c * transform.c) );};
static inline CGFloat CGAffineTransformGetScaleY(CGAffineTransform transform) {return sqrtf( (transform.b * transform.b) + (transform.d * transform.d) );};
static inline CGFloat CGAffineTransformGetShearX(CGAffineTransform transform) {return transform.b;};
static inline CGFloat CGAffineTransformGetShearY(CGAffineTransform transform) {return transform.c;};
static inline CGFloat CGPointAngleBetweenPoints(CGPoint first, CGPoint second)
{
CGFloat dy = second.y - first.y;
CGFloat dx = second.x - first.x;
return atan2f(dy, dx);
}

static inline CGFloat CGAffineTransformGetRotation(CGAffineTransform transform)
{
// No exact way to get rotation out without knowing order of all previous operations
// So, we'll cheat. We'll apply the transformation to two points and then determine the
// angle betwen those two points

CGPoint testPoint1 = CGPointMake(-100.f, 0.f);
CGPoint testPoint2 = CGPointMake(100.f, 0.f);
CGPoint transformed1 = CGPointApplyAffineTransform(testPoint1, transform);
CGPoint transformed2 = CGPointApplyAffineTransform(testPoint2, transform);
return CGPointAngleBetweenPoints(transformed1, transformed2);
}


#ifdef __cplusplus
}
#endif

#endif

Saturday, October 9, 2010

Tile Cutter Open Sourced

I have open-sourced Tile Cutter under the MIT license and put it on Github as a public project. This application was written extremely quickly (less than half a day) because I needed the functionality for a client project, so there are several things about this application that are klugey, including some uncomfortable crossing of the boundaries between what a view should do and what a controller should.

But, it works, and I don't see myself having time to go clean it up anytime soon, so I'm donating it to the public good, warts and all. I welcome back any enhancements, bug fixes, or modifications, but you are not required to give anything back under the license.

Wednesday, December 30, 2009

Updated Navigation-Based Core Data Application Template

Periodically, while working on the Core Data chapters in More iPhone 3 Development, I would update my copy of Apple's provided Navigation-Based Core Data Application project template with logic to handle different common scenarios. While working on the previous blog post, I made some more tweaks to the template to better handle object changes that trigger managed objects to move between sections when specifying a sectionNameKeyPath. You can find the most recent version of this project template here, and I've included the four delegate methods from the template at the end of this blog post.

I've really tried to give NSFetchedResultsController the benefit of the doubt. It's a really, really great concept and when it works, I really like it, but the execution is way below Apple's normal level of code quality. All of the code I've added to this template is needed simply to handle typical use-cases supposedly supported by this class… things that should be handled out of the box without any need for additional code. In this particular case, the delegate methods should really only be required for handling extraordinary or unusual scenarios.

If I have time, I may try to encapsulate this logic into a subclass of NSFetchedResultsController, but for the time being, if you use this template or the delegate methods at the end of this posting, it should take care of the bulk of situations for you automatically. I suspect that there will be a few more tweaks to this code as I use it in real-world code, but if there are, I'll post the changes and a new version of the template here. In any case, this template is a far better starting point than Apple's provided template and may just keep you from pulling all your hair out.

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate: {
NSString *sectionKeyPath = [controller sectionNameKeyPath];
if (sectionKeyPath == nil)
break;
NSManagedObject *changedObject = [controller objectAtIndexPath:indexPath];
NSArray *keyParts = [sectionKeyPath componentsSeparatedByString:@"."];
id currentKeyValue = [changedObject valueForKeyPath:sectionKeyPath];
for (int i = 0; i < [keyParts count] - 1; i++) {
NSString *onePart = [keyParts objectAtIndex:i];
changedObject = [changedObject valueForKey:onePart];
}

sectionKeyPath = [keyParts lastObject];
NSDictionary *committedValues = [changedObject committedValuesForKeys:nil];

if ([[committedValues valueForKeyPath:sectionKeyPath] isEqual:currentKeyValue])
break;

NSUInteger tableSectionCount = [self.tableView numberOfSections];
NSUInteger frcSectionCount = [[controller sections] count];
if (tableSectionCount != frcSectionCount) {
// Need to insert a section
NSArray *sections = controller.sections;
NSInteger newSectionLocation = -1;
for (id oneSection in sections) {
NSString *sectionName = [oneSection name];
if ([currentKeyValue isEqual:sectionName]) {
newSectionLocation = [sections indexOfObject:oneSection];
break;
}

}

if (newSectionLocation == -1)
return; // uh oh

if (!((newSectionLocation == 0) && (tableSectionCount == 1) && ([self.tableView numberOfRowsInSection:0] == 0)))
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:newSectionLocation] withRowAnimation:UITableViewRowAnimationFade];
NSUInteger indices[2] = {newSectionLocation, 0};
newIndexPath = [[[NSIndexPath alloc] initWithIndexes:indices length:2] autorelease];
}

}

case NSFetchedResultsChangeMove:
if (newIndexPath != nil) {

NSUInteger tableSectionCount = [self.tableView numberOfSections];
NSUInteger frcSectionCount = [[controller sections] count];
if (frcSectionCount >= tableSectionCount)
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:[newIndexPath section]] withRowAnimation:UITableViewRowAnimationNone];
else
if (tableSectionCount > 1)
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationNone];


[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject:newIndexPath]
withRowAnimation: UITableViewRowAnimationRight
]
;

}

else {
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationFade];
}

break;
default:
break;
}

}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {

case NSFetchedResultsChangeInsert:
if (!((sectionIndex == 0) && ([self.tableView numberOfSections] == 1)))
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
if (!((sectionIndex == 0) && ([self.tableView numberOfSections] == 1) ))
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeMove:
break;
case NSFetchedResultsChangeUpdate:
break;
default:
break;
}

}

Thursday, November 19, 2009

Update to the MPMediaItemCollection Category

While testing and debugging, I made a few tweaks to this category. I still haven't added reordering, but I think the methods that are here are now pretty solid. If you downloaded the earlier one, you probably want to grab the updated version, as it now codes defensively so problems aren't caused when a collection becomes empty by deletion (media item collections can't be created without at least one media item).

MPMediaItemCollection-Utils.h
#import <Foundation/Foundation.h>
#import <MediaPlayer/MediaPlayer.h>

@interface MPMediaItemCollection(Utils)
/** Returns the first media item in the collection
*/

- (MPMediaItem *)firstMediaItem;

/** Returns the last media item in the collection
*/

- (MPMediaItem *)lastMediaItem;

/** This method will return the item in this media collection at a specific index
*/

- (MPMediaItem *)mediaItemAtIndex:(NSUInteger)index;

/** Given a particular media item, this method will return the next media item in the collection.
If there are multiple copies of the same media item in the list, it will return the one
after the first occurrence.
*/

- (MPMediaItem *)mediaItemAfterItem:(MPMediaItem *)compare;

/** Returns the title of the media item at a given index.
*/

- (NSString *)titleForMediaItemAtIndex:(NSUInteger)index;

/** Returns YES if the given media item occurs at least once in this collection
*/

- (BOOL)containsItem:(MPMediaItem *)compare;

/** Creates a new collection by appending otherCollection to the end of this collection
*/

- (MPMediaItemCollection *)collectionByAppendingCollection:(MPMediaItemCollection *)otherCollection;

/** Creates a new collection by appending an array of media items to the end of this collection
*/

- (MPMediaItemCollection *)collectionByAppendingMediaItems:(NSArray *)items;

/** Creates a new collection by appending a single media item to the end of this collection
*/

- (MPMediaItemCollection *)collectionByAppendingMediaItem:(MPMediaItem *)item;

/** Creates a new collection based on this collection, but excluding the specified items.
*/

- (MPMediaItemCollection *)collectionByDeletingMediaItems:(NSArray *)itemsToRemove;

/** Creates a new collection based on this collection, but which doesn't include the specified media item.
*/

- (MPMediaItemCollection *)collectionByDeletingMediaItem:(MPMediaItem *)itemToRemove;

/** Creates a new collection based on this collection, but excluding the media item at the specified index
*/

- (MPMediaItemCollection *)collectionByDeletingMediaItemAtIndex:(NSUInteger)index;

/** Creates a new collection, based on this collection, but excluding the media items starting with
(and including) the objects at index from and ending with (and including) to.
*/

- (MPMediaItemCollection *)collectionByDeletingMediaItemsFromIndex:(NSUInteger)from toIndex:(NSUInteger)to;
@end



MPMediaItemCollection-Utils.m
#import "MPMediaItemCollection-Utils.h"

@implementation MPMediaItemCollection(Utils)
- (MPMediaItem *)firstMediaItem {
return [[self items] objectAtIndex:0];
}


- (MPMediaItem *)lastMediaItem {
return [[self items] lastObject];
}


- (MPMediaItem *)mediaItemAtIndex:(NSUInteger)index {
return [[self items] objectAtIndex:index];
}


- (MPMediaItem *)mediaItemAfterItem:(MPMediaItem *)compare {
NSArray *items = [self items];

for (MPMediaItem *oneItem in items) {
if ([oneItem isEqual:compare]) {
// If last item, there is no index + 1
if (![[items lastObject] isEqual: oneItem])
return [items objectAtIndex:[items indexOfObject:oneItem] + 1];
}

}

return nil;
}


- (NSString *)titleForMediaItemAtIndex:(NSUInteger)index {
MPMediaItem *item = [[self items] objectAtIndex:index];
return [item valueForProperty:MPMediaItemPropertyTitle];
}


- (BOOL)containsItem:(MPMediaItem *)compare {
NSArray *items = [self items];

for (MPMediaItem *oneItem in items) {
if ([oneItem isEqual:compare])
return YES;
}

return NO;
}


- (MPMediaItemCollection *)collectionByAppendingCollection:(MPMediaItemCollection *)otherCollection {
return [self collectionByAppendingMediaItems:[otherCollection items]];
}


- (MPMediaItemCollection *)collectionByAppendingMediaItems:(NSArray *)items {
if (items == nil || [items count] == 0)
return nil;
NSMutableArray *appendCollection = [[[self items] mutableCopy] autorelease];
[appendCollection addObjectsFromArray:items];
return [MPMediaItemCollection collectionWithItems:appendCollection];
}


- (MPMediaItemCollection *)collectionByAppendingMediaItem:(MPMediaItem *)item {
if (item == nil)
return nil;

return [self collectionByAppendingMediaItems:[NSArray arrayWithObject:item]];
}


- (MPMediaItemCollection *)collectionByDeletingMediaItems:(NSArray *)itemsToRemove {
if (itemsToRemove == nil || [itemsToRemove count] == 0)
return [[self copy] autorelease];
NSMutableArray *items = [[[self items] mutableCopy] autorelease];
[items removeObjectsInArray:itemsToRemove];
return [MPMediaItemCollection collectionWithItems:items];
}


- (MPMediaItemCollection *)collectionByDeletingMediaItem:(MPMediaItem *)itemToRemove {
if (itemToRemove == nil)
return [[self copy] autorelease];

NSMutableArray *items = [[[self items] mutableCopy] autorelease];
[items removeObject:itemToRemove];
return [MPMediaItemCollection collectionWithItems:items];
}


- (MPMediaItemCollection *)collectionByDeletingMediaItemAtIndex:(NSUInteger)index {
NSMutableArray *items = [[[self items] mutableCopy] autorelease];
[items removeObjectAtIndex:index];
return [items count] > 0 ? [MPMediaItemCollection collectionWithItems:items] : nil;
}


- (MPMediaItemCollection *)collectionByDeletingMediaItemsFromIndex:(NSUInteger)from toIndex:(NSUInteger)to {

// Ensure from is before to
if (to < from) {
NSUInteger temp = from;
to = from;
from = temp;
}


NSMutableArray *items = [[[self items] mutableCopy] autorelease];
[items removeObjectsInRange:NSMakeRange(from, to - from)];
return [MPMediaItemCollection collectionWithItems:items];
}

@end