将视图移动到某一个随机位置时,必须考虑这么几个问题:
#import
@interface
- (BOOL) canMoveToCenter: (CGPoint) aCenter inView: (UIView *) aView withInsets: (UIEdgeInsets) insets;
- (BOOL) canMoveToCenter: (CGPoint) aCenter inView: (UIView *) aView withInset: (float) inset;
- (BOOL) canMoveToCenter: (CGPoint) aCenter inView: (UIView *) aView;
- (CGPoint) centerInView: (UIView *) aView withHorizontalPercent: (float) h withVerticalPercent: (float) v;
- (CGPoint) centerInSuperviewWithHor
- (CGPoint) randomCenterInView: (UIView *) aView withInsets: (UIEdgeInsets) insets;
- (CGPoint) randomCenterInView: (UIView *) aView withInset: (float) inset;
- (void) moveToRandomLocationInVi
- (void) moveToRandomLocationInSu
@end
#import
// This is a private version of the function that appears in my UIView Frame category
// It's included here as a private function to avoid requiring the other file
CGRect rectWithCenter(CGRect rect, CGPoint center)
{
CGRect newrect = CGRectZero;
newrect.origin.x = center.x-CGRectGetMidX(rect);
newrect.origin.y = center.y-CGRectGetMidY(rect);
newrect.size = rect.size;
return
}
@implementation
#pragma mark Bounded Placement
- (BOOL) canMoveToCenter: (CGPoint) aCenter inView: (UIView *) aView withInsets: (UIEdgeInsets) insets
{
CGRect container = UIEdgeInsetsInsetRect(aView.bounds, insets);
return
}
- (BOOL) canMoveToCenter: (CGPoint) aCenter inView: (UIView *) aView withInset: (float) inset
{
UIEdgeInsets insets = UIEdgeInsetsMake(inset, inset, inset, inset);
return
}
- (BOOL) canMoveToCenter: (CGPoint) aCenter inView: (UIView *) aView
{
return
}
#pragma mark Percent Displacement
// Move view into place as a percentage-based displacement
- (CGPoint) centerInView: (UIView *) aView withHorizontalPercent: (float) h withVerticalPercent: (float) v
{
// Move in by the inset amount and then by size of the subview
CGRect baseRect = aView.bounds;
CGRect subRect = CGRectInset(baseRect,
// Return a point that is h% horizontal and v% vertical
float
float
return
}
- (CGPoint) centerInSuperviewWithHor
{
return
}
#pragma mark Random
// Make sure you've run srandom() elsewhere in the program
// Thanks to August Joki and manitoba98
- (CGPoint) randomCenterInView: (UIView *) aView withInsets: (UIEdgeInsets) insets
{
// Move in by the inset amount and then by size of the subview
CGRect innerRect = UIEdgeInsetsInsetRect([aView bounds], insets);
CGRect subRect = CGRectInset(innerRect,
// Return a random point
float
float
return
}
- (CGPoint) randomCenterInView: (UIView *) aView withInset: (float) inset
{
UIEdgeInsets insets = UIEdgeInsetsMake(inset, inset, inset, inset);
return
}
- (void) moveToRandomLocationInVi
{
if
{
self.center = [self
return;
}
// Why 0.3f seconds? Because that is the time used to display a keyboard
CGContextRef context = UIGraphicsGetCurrentCont
[UIView beginAnimations:nil
[UIView setAnimationCurve:UIViewAnimationCurveEase
[UIView setAnimationDuration:0.3f];
self.center = [self
[UIView commitAnimations];
}
- (void) moveToRandomLocationInSu
{
[self
}
@end