I have an UIView which is a picture frame hanging on a nail from a string. I'm trying to rotate the view around its anchorPoint to keep the view's bottom flat(parallel with the ground) regardless of the devices rotation. I'm trying to achieve this by using CMMotionManager and UIDynamics and can't seem to get anything working properly today.
Here's some code...
- (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)];
}];
}
I've tried motion.gravity.x as an angularForce on itemBehavior to verify the forces are returning correctly, and the view rotates the correct direction, which everything seems to be fine. The problem with this approach is that the view continues to rotate since theres constant force being applied. Using setGravityDirection: results with the view anchored with no rotation. Some help would be much appreciated! thanks!
try to change the NSOperationQueue to the mainQueue. This helped me.
[self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
[self.gravityBehavior setGravityDirection:CGVectorMake(motion.gravity.x, -(motion.gravity.y))];
}];
Hope this helped you too.
Related
I'm trying to use UIKit Dynamics to implement swipe-to-delete on my UICollectionViewCells. Things aren't going as planned, though. Here's the code in UICollectionViewCell's awakeFromNib method:
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.contentView];
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:#[self.contentView]];
[collisionBehavior setTranslatesReferenceBoundsIntoBoundaryWithInsets:UIEdgeInsetsMake(0, 0, 0, -20)];
[self.animator addBehavior:collisionBehavior];
self.gravityBehavior = [[UIGravityBehavior alloc] initWithItems:#[self.containerView]];
self.gravityBehavior.gravityDirection = CGVectorMake(0, 0);
[self.animator addBehavior:self.gravityBehavior];
self.pushBehavior = [[UIPushBehavior alloc] initWithItems:#[self.containerView] mode:UIPushBehaviorModeInstantaneous];
self.pushBehavior.magnitude = 0.0f;
self.pushBehavior.angle = 0.0f;
[self.animator addBehavior:self.pushBehavior];
UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:#[self.containerView]];
itemBehavior.elasticity = 0.45f;
[self.animator addBehavior:itemBehavior];
But this is not working so well. This is what happens at launch with the code above:
That behavior seems to be mostly generated by UICollisionBehavior. If I comment it out, the container view with everything that's not the red background doesn't animate, but does show up offset a few points to the left.
Am I right to try to implement this within UICollectionViewCell? What's the right way of doing this?
Thanks!
Never mind. It was pretty much a typo. The collision behavior was attached to the wrong object.
The right code should be
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:#[self.containerView]];
[collisionBehavior setTranslatesReferenceBoundsIntoBoundaryWithInsets:UIEdgeInsetsMake(0, 0, 0, -20)];
[self.animator addBehavior:collisionBehavior];
I'm searching a programmatic solution to following problem: I want to draw a custom overlay on camera (iOS). And I want it to be vertically central on the camera output view. I've accomplished to draw my custom view centrically relatively to the screen, but not to the camera picture.
To do that, I need to get the size of the top black bar. How can I get it?
The size of top and bottom bars are not equal, that's why the picture I get has this annoying y-offset to the bottom.
Notice the offset of the resulting pic:
OK guys, I ended up using AVFoundation, which in the end was even better solution as it is customizable. I had self.fullScreenImageView and self.previewLayer as class properties. You need to write your own code implementing the buttons and the code of taking a camera shot. Here you can play around with the quality of the photo to get optimal size/quality. I used 0.6, but you can choose whatever you want.
#import MobileCoreServices;
#import AVFoundation;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view setBackgroundColor:[UIColor blackColor]];
self.fullScreenImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
self.fullScreenImageView.contentMode = UIViewContentModeScaleAspectFit;
[self.fullScreenImageView setCenter:self.view.center];
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
captureSession.sessionPreset = AVCaptureSessionPresetHigh;
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (captureDevice) {
NSError *error;
AVCaptureDeviceInput *captureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:captureDevice error:&error];
if (!error && [captureSession canAddInput:captureDeviceInput]) {
[captureSession addInput:captureDeviceInput];
}
}
AVCaptureStillImageOutput *stillImageOutput = [AVCaptureStillImageOutput new];
[stillImageOutput setOutputSettings:#{AVVideoCodecKey : AVVideoCodecJPEG,
AVVideoQualityKey : #(0.6)}];
[captureSession addOutput:stillImageOutput];
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
self.previewLayer.frame = self.fullScreenImageView.bounds;
self.previewLayer.position = CGPointMake(CGRectGetMidX(self.fullScreenImageView.bounds), CGRectGetMidY(self.fullScreenImageView.bounds));
[self.fullScreenImageView.layer addSublayer:self.previewLayer];
[captureSession startRunning];
CGRect circleRect = CGRectMake(0, (self.fullScreenImageView.bounds.size.height - self.fullScreenImageView.bounds.size.width) / 2, self.fullScreenImageView.bounds.size.width, self.fullScreenImageView.bounds.size.width);
UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:circleRect];
CAShapeLayer *ringLayer = [CAShapeLayer layer];
ringLayer.path = circle.CGPath;
ringLayer.fillColor = nil;
ringLayer.strokeColor = [UIColor redColor].CGColor;
ringLayer.lineWidth = 2.0;
ringLayer.lineDashPattern = #[#5.0, #10.0];
[self.fullScreenImageView.layer addSublayer:ringLayer];
[self.navigationController setToolbarHidden:NO];
[self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];
[self.navigationController.toolbar setTintColor:[UIColor whiteColor]];
// Add here some buttons, which are standard UIBarButtonItems
[self.view addSubview:self.fullScreenImageView];
}
I want to replicate the attachment behavior of UITableViewCell, just like what Apple made in the Messages App.
I tried subclassing UITableViewCell and here is the code :
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self];
self.attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.superview attachedToAnchor:self.superview.center];
[self.attachmentBehavior setFrequency:4.5];
[self.attachmentBehavior setDamping:0.3];
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:#[self]];
//gravity direction: right
[gravityBehavior setGravityDirection:CGVectorMake(0.0, -0.8)];
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:#[self]];
[collisionBehavior addBoundaryWithIdentifier:#"topBoundary" fromPoint:CGPointMake(0.0f, 0.0f) toPoint:CGPointMake(self.contentView.frame.size.width, 0.0f)];
[collisionBehavior addBoundaryWithIdentifier:#"bottomBoundary" fromPoint:CGPointMake(0.0f, self.contentView.frame.size.height) toPoint:CGPointMake(self.contentView.frame.size.width, self.contentView.frame.size.height)];
UIDynamicItemBehavior* itemBehaviour = [[UIDynamicItemBehavior alloc] initWithItems:#[self]];
itemBehaviour.elasticity = 0.9;
[self.animator addBehavior:itemBehaviour];
[self.animator addBehavior:gravityBehavior];
[self.animator addBehavior:collisionBehavior];
But, it doesn't seem to work.
Can someone help me to reproduce the same functionality as the Messages app please ?
Edited :
I know that Apple uses UICollectionView instead tableview for the Messages App, but i hope there should be a way to do this kind of functionality through tableview also.
I am trying my hand at UIKit Dynamics and am trying to create a simple animation with a ball that bounces off of the screen boundaries.
As shown in the code below, when I add the gravity behavior the ball seems to go off the screen as expected. But when I add the collision behavior, nothing happens. The ball stays as is at the center of the screen where I have drawn it initially.
I also trie to add a push behavior with continuous push force option, but still the ball does not move.
#implementation TAMViewController
UIDynamicAnimator* animator;
UIGravityBehavior* gravity;
UICollisionBehavior* collision;
UIPushBehavior* push;
- (void)viewDidLoad
{
[super viewDidLoad];
TAMBouncyView *ballView = [[TAMBouncyView alloc] initWithFrame:CGRectMake(0,0,
self.view.frame.size.width,
self.view.frame.size.height)];
[self.view addSubview:ballView];
animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
gravity = [[UIGravityBehavior alloc] initWithItems:#[ballView]];
[animator addBehavior:gravity];
collision = [[UICollisionBehavior alloc] initWithItems:#[ballView]];
[collision setTranslatesReferenceBoundsIntoBoundary:YES];
[animator addBehavior:collision];
push = [[UIPushBehavior alloc] initWithItems:#[ballView]
mode:UIPushBehaviorModeContinuous];
[animator addBehavior:push];
// Do any additional setup after loading the view, typically from a nib.
}
I'm pretty new on iOs programming. And I'm stuck at a (i'm sure) very simple issue.
Don't know what i'm doing wrong...
-My viewDidLoad:
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 768, 1024);
UIView *uno=[[[UIView alloc] initWithFrame:frame] autorelease];
UIImageView *mainView = [[[UIImageView alloc] initWithFrame:frame] autorelease];
mainView.image = [UIImage imageNamed:#"photo.jpg"];
[uno addSubview:mainView];
UIView *dos=[[[UIView alloc] initWithFrame:frame] autorelease];
UIImageView *mainViewDos = [[[UIImageView alloc] initWithFrame:frame] autorelease];
mainViewDos.image = [UIImage imageNamed:#"Default.png"];
[dos addSubview:mainViewDos];
//
[self.view addSubview:uno];
//
[self anima:uno:dos];
And my anima method:
-(void) anima:(UIView *)uno:(UIView *)dos{
[UIView transitionFromView:uno
toView:dos
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
}
It changes the view but without transition...
Thanks
You can't perform an animation within your viewDidLoad--the view changes you make in there are all executed before the view is actually displayed, which is what you're seeing.
Are you trying to show this animation when the view is first displayed? If so, you can get it to work by putting the animation on a timer. Note that with this approach, you'll also have to refactor your anima method a bit to take a single argument.
In your viewDidLoad:
NSDictionary *views = [NSDictionary dictionaryWithObjectsAndKeys:uno, #"uno", dos, #"dos", nil];
[self performSelector:#selector(anima) withObject:views afterDelay:0.1];
Then change your anima method to:
-(void) anima:(NSDictionary *)views {
[UIView transitionFromView:[views objectForKey:#"uno"]
toView:[views objectForKey:#"dos"]
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
}