gpt4 book ai didi

ios - 如果不将UIgravity声明为属性,原因是什么?

转载 作者:行者123 更新时间:2023-12-01 19:04:42 25 4
gpt4 key购买 nike

好的,所以我开始学习UIDynamics,只想在小盒子上解决它。
我在ViewController.m中声明了UIGravityBehaviour和UIDynamicsBehaviour并对UIView进行了编码,但是当我执行代码时,它不起作用。
但是正如我在.h文件中将它们声明为@property一样,该程序也可以正常工作。
背后的原因是什么?
将其声明为@property和不将其声明为@property有什么区别?

这是我的文件。

在使用@property之前

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

ViewController.m
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
UIView* box = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 100, 100)];
box.backgroundColor = [UIColor blackColor];
[self.view addSubview:box];
UIDynamicAnimator* myAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIGravityBehavior* gravityBehaviour = [[UIGravityBehavior alloc] initWithItems:@[box]];
[myAnimator addBehavior: gravityBehaviour];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

声明为@property后

ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong,nonatomic) UIDynamicAnimator* myAnimator ;
@property (strong,nonatomic) UIGravityBehavior *gravityBehavior;
@property (strong,nonatomic) UICollisionBehavior *collisionBehavior ;
@end

ViewController.m
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
UIView* box = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 100, 100)];
box.backgroundColor = [UIColor blackColor];
[self.view addSubview:box];
self.myAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
self.gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[box]];
self.collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[box]];
self.collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[self.myAnimator addBehavior:self.gravityBehavior];
[self.myAnimator addBehavior:self.collisionBehavior];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

最佳答案

您不必将UIGravityBehavior移至某个属性,只需将UIDynamicAnimator移至该属性即可。但是,如果您要删除特定的重力行为而不是一次删除所有重力行为,则无论如何都是个好主意。

关于ios - 如果不将UIgravity声明为属性,原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20451598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com