문자열에서 못을 붙이는 그림 프레임 인 UIView가 있습니다. 장치의 회전에 관계없이보기의 바닥을 평평하게 (지면과 평행하게) 유지하려면 앵커 포인트 주위로보기를 회전하려고합니다. CMMotionManager 및 UIDynamics를 사용하여이 작업을 수행하려고 시도하고 있으며 현재 제대로 작동하는 것으로 보이지 않습니다. 여기CMMotionManager에서 UIDynamics 및 중력을 사용하여 UIView 회전하기
- (void)viewDidLoad
{
[super viewDidLoad];
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
CGRect pictureFrameBounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.view.bounds)/2.5f, CGRectGetHeight(self.view.bounds)/5);
UIView *pictureFrame = [[UIView alloc] initWithFrame:pictureFrameBounds];
[pictureFrame setBackgroundColor:[UIColor redColor]];
[self.view addSubview:pictureFrame];
self.gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[pictureFrame]];
self.collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[pictureFrame]];
[self.collisionBehavior setTranslatesReferenceBoundsIntoBoundary:YES];
[self.animator addBehavior:self.collisionBehavior];
CGPoint anchorPoint = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetHeight(self.view.bounds)/8);
self.attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:pictureFrame
offsetFromCenter:UIOffsetMake(0.0f, -CGRectGetHeight(pictureFrame.bounds)/2.5f)
attachedToAnchor:anchorPoint];
[self.attachmentBehavior setDamping:0];
[self.attachmentBehavior setLength:0];
[self.attachmentBehavior setFrequency:0];
[self.animator addBehavior:self.attachmentBehavior];
UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[pictureFrame]];
[itemBehavior setAllowsRotation:YES];
[self.animator addBehavior:itemBehavior];
NSOperationQueue* motionQueue = [NSOperationQueue new];
self.motionManager = [[CMMotionManager alloc] init];
[self.motionManager startDeviceMotionUpdatesToQueue:motionQueue
withHandler:^(CMDeviceMotion *motion, NSError *error) {
[self.gravityBehavior setGravityDirection:CGVectorMake(motion.gravity.x, motion.gravity.y)];
}];
}
내가 힘이 제대로 반환하는 확인 itemBehavior에 angularForce로 motion.gravity.x 시도했습니다 ... 일부 코드, 그리고 뷰는 모든 것이 잘 될 것 같다 올바른 방향을 회전 . 이 방법의 문제점은 일정한 힘이 가해지기 때문에 뷰가 계속 회전한다는 것입니다. setGravityDirection 사용 : 뷰가 회전없이 고정 된 결과. 어떤 도움을 많이 주시면 감사하겠습니다! 감사!