Skip to content

Instantly share code, notes, and snippets.

@jontelang
Created June 13, 2014 16:20
Show Gist options
  • Save jontelang/de777733f5883e2737b8 to your computer and use it in GitHub Desktop.
Save jontelang/de777733f5883e2737b8 to your computer and use it in GitHub Desktop.
CodeWhichILike
/*
*
* The code below simply returns a corrected frame for an object that has been "thrown" (with a flick
* of the finger), and that object is then animated to that frame elsewhere in this case. It also
* decides where it should end up based on some variables like if it should be 50% hidden or nog (not
* shown in video below).
*
* You might now think "that's not impressive" and I agree, it is pretty easy code and not even that
* pretty. I'll be honest, I don't think I have any code that will blow your mind, I'm not that good
* of a programmer.
*
* I choose this code because of that it gave back to the application. It made an, at the time, stiff
* and boring application suddenly not just fluid, but actually FUN, to use. It is very satisfying to
* throw the object around and see it bounce back. I actually got emails about it where they
* complimented me on it, and that's when I became proud of this little snippet of code.
*
* Look at the result here http://gfycat.com/CompassionateFrenchAlligator
*
* If you're wondering what it is it is "Assistive" which in turn is a/my re-implementation of the
* stock iOS "Assistive Touch" which I felt was not sufficient for me.
*
*/
-(CGRect)correctedNewRect:(CGRect)rect shouldCareAboutSnappingBOOL:(BOOL)shouldCareAboutSnapping allowOutside:(BOOL)allowOutside
{
float margin = 2;
if(!CGRectContainsRect(self.bounds,mainView.frame) && allowOutside){
margin = -(mainView.frame.size.width/2);
}
// Correct LEFT
if( rect.origin.x < margin )
{
rect.origin.x = margin;
}
// Correct RIGHT
else if (rect.origin.x + rect.size.width > [UIScreen mainScreen].bounds.size.width - margin )
{
rect.origin.x = [UIScreen mainScreen].bounds.size.width - margin - rect.size.width;
}
// Correct TOP
if (rect.origin.y < margin )
{
rect.origin.y = margin;
}
// Correct BOTTOM
else if( rect.origin.y + rect.size.height > [UIScreen mainScreen].bounds.size.height - margin )
{
rect.origin.y = [UIScreen mainScreen].bounds.size.height - margin - rect.size.height;
}
if(shouldCareAboutSnapping)
{
if(doesSnap)
{
float area = [UIScreen mainScreen].bounds.size.height/6;
if(rect.origin.y < area)
{
rect.origin.y = margin;
}
else if( rect.origin.y > [UIScreen mainScreen].bounds.size.height - rect.size.height - area)
{
rect.origin.y = [UIScreen mainScreen].bounds.size.height - rect.size.height - margin;
}
else
{
if(rect.origin.x + rect.size.width/2 < [UIScreen mainScreen].bounds.size.width/2)
{
rect.origin.x = margin;
}
else
{
rect.origin.x = [UIScreen mainScreen].bounds.size.width - rect.size.width - margin;
}
}
}
}
return rect;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment