我的代码将 UIImage 放到屏幕底部。但是当我将相同的代码放在isMyUIViewControllerDelegateTriggered中时它不起作用。这不是同一个线程吗??
MyUIViewController.m
@interface MyUIViewController () <UIDynamicAnimatorDelegate>
@property (strong, nonatomic) UIDynamicAnimator *animator;
@property (strong, nonatomic) DropitBehavior *dropitBehavior;
@property (strong, nonatomic) IBOutlet UIView *jobLoadView;
@end
@synthesize someUIImage;
id refToSelf; /* Tried this to have a ref to the UIView, but doesn't work. */
static DropitBehavior * dropit;
- (void)viewDidLoad
{
refToSelf = self;
dropit = [[DropitBehavior alloc] init];
/* Placed inhere, this code works!
DropitBehavior * dropit = [refToSelf dropitBehavior];
[dropit addItem:someUIImage]; */
startupManager = [[StartupManager alloc] init];
[startupManager setDelegate:refToSelf];
[startupManager start];
}
- (DropitBehavior *)dropitBehavior
{
if (!_dropitBehavior) {
_dropitBehavior = [[DropitBehavior alloc] init];
[self.animator addBehavior:_dropitBehavior];
}
return _dropitBehavior;
}
- (UIDynamicAnimator *)animator
{
if (!_animator) {
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.jobLoadView];
_animator.delegate = self;
}
return _animator;
}
-(void)isStartupManagerDelegateTriggered:(NSString *) errorcode {
/* The startup manager code triggers 5 time, so I can make a loading effect. */
/* Doesn't work when I place the code inhere. */
dropit = [refToSelf dropitBehavior];
if ([dropit isKindOfClass:[DropitBehavior class]]) {
NSLog(@"is kind of class drop it ");
[dropit addItem:someUIImage];
}
}
DropitBehavior.m
@interface DropitBehavior()
@property (strong, nonatomic) UIGravityBehavior *gravity;
@property (strong, nonatomic) UICollisionBehavior *collider;
@end
@implementation DropitBehavior
/* Singleton */
static DropitBehavior *sharedSingleton;
+ (void)initialize {
static BOOL initialized = NO;
if(!initialized)
{
initialized = YES;
sharedSingleton = [[DropitBehavior alloc] init];
}
}
- (UIGravityBehavior *)gravity
{
if (!_gravity) {
_gravity = [[UIGravityBehavior alloc] init];
_gravity.magnitude = 0.9;
}
return _gravity;
}
- (UICollisionBehavior *)collider
{
if (!_collider) {
_collider = [[UICollisionBehavior alloc] init];
_collider.translatesReferenceBoundsIntoBoundary = YES;
}
return _collider;
}
- (void)addItem:(id <UIDynamicItem>)item
{
[self.gravity addItem:item];
[self.collider addItem:item];
}
- (instancetype)init
{
self = [super init];
[self addChildBehavior:self.gravity];
[self addChildBehavior:self.collider];
return self;
}
@end
启动管理器.m
@implementation StartupManager
JSONParser * jsonParser;
HTTPConnection * httpConnection;
static StartupManager *sharedSingleton;
@synthesize delegate;
+ (void)initialize {
static BOOL initialized = NO;
if(!initialized) {
initialized = YES;
sharedSingleton = [[StartupManager alloc] init];
jsonParser = [[JSONParser alloc] init];
httpConnection = [[HTTPConnection alloc] init];
}
}
- (void) doingSomeStuff {
[httpConnection setDelegate:self];
[httpConnection postRequestToSomeServer:@"some.instance.inthe.cloud";
}
-(void)isHTTPConnectionDelegateTriggered:(NSData *)jsonPost {
[jsonParser setDelegate:self];
[jsonParser parseJSON:jsonPost];
}
-(void)isJSONParserDelegateTriggered:(NSString *) message {
*/ this gets called periodically and that has to do a loading screen animation. /*
[delegate isStartupManagerDelegateTriggered:message];
}
启动管理器.h
@class StartupManager;
@protocol StartupManagerDelegate
@required
-(void)isStartupManagerDelegateTriggered:(NSString *) errorcode;
@end
@interface StartupManager : NSObject <JSONParserDelegate, HTTPConnectionDelegate>
@property (nonatomic, assign) id<StartupManagerDelegate> delegate;
- (void) doingSomeStuff;
有没有人可以指出我正确的方向?不然我得问爷爷了。