0

I want to bind two views to a button's border like a mask. The effect I want is seen in the image below

image demo

So I have a custom controller that uses a PageViewController to swipe between two UIViewControllers. I also have two views (the red one and the blue one). I can track the direction and degree of the page turning, so I can link that to the movement of the two views. All I need to do is apply a mask to the moving boxes so that all the movement stays within the button.

If anyone knows how to do this, I'd be eternally grateful for the information!

Undo
  • 25,519
  • 37
  • 106
  • 129
cwRichardKim
  • 1,030
  • 1
  • 9
  • 15

3 Answers3

1

My understanding is that you have a view, then you want to transition between its two subviews with a sliding animation. This is fairly straight forward.

First set "Clip Subviews" to true (either in storyboard editor or setting view.clipToBounds=true). If you want a sphere like you show then set layer.cornerRadius = view.frame.size.width/2

The you can just set the frames of your subviews- either animating them with UIView animateWithDuration or setting the frames yourself in your animation loop.

The former in code (for sliding the first element to the left and right element in):

[UIView animateWithDuration:duration animations:^{
    [subview1 setFrame:CGRectMake(-subview1.frame.size.width, 0, subview1.frame.size.width, subview1.frame.size.height)];
    [subview2 setFrame:CGRectMake(0, 0, subview2.frame.size.width, subview2.frame.size.height)];
}];

The reverse animation is similar. If you're doing it in your own animation loop (which sounds like what you want) then you can do the interpolation yourself pretty easily.

If you want to be more advanced with your mask (i.e. based on an image) then look at something like this: How to mask UIViews in iOS

Community
  • 1
  • 1
Bourne
  • 336
  • 3
  • 4
  • great idea! I didn't think to add the views on top of the button. do you think that'll interfere with the functionality of the button? – cwRichardKim Jan 28 '15 at 22:58
  • I'm not sure. If the two moving views are subviews of the button then I expect the button to act just fine. If you're moving the frame of a button around then that frame will always be what activates the button. Of course, if you ever add a view over a button or are worried about overlap you can set userInteractionEnabled to false- this will make all touches go through that element. Of course don't disable user interaction on the buttons – Bourne Jan 29 '15 at 01:41
  • yea I'm more concerned with the animations. it looks like when you customize a button this much it doesn't give you any tap feedback so i'm going to have to code that manually. Thanks again for the idea! – cwRichardKim Jan 29 '15 at 04:53
0

This solution may not be the optimized one, but do the trick you want. First i wanna make sure that i understand your requirement. I understand that you wanna go away to left the red image & bring in the right blue image in the button with the sliding effect.

If my understanding meet your requirement,Here is the trick

  • First decide which portion of red & blue image you needed. That must be easy as you know the direction & degree of the turning the UIPageViewController.
  • Then Crop that decided portions from the red & blue image using the code here.
  • Now draw both resulted cropped image on the coreGraphics side by side (red on the left & blue on the right) & retrieve the final resulted image.
  • set the image to the UIButton.
  • Finally to acquire the round shape you can set the radius property of layer property of the button.

For the third step, how you can you draw image on coreGraphics & retrieve apple give a detail explanation here

You have to do all these steps each time you wanna update the image of the UIButton. There may be a other good way to do this :)

Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
-2

You can use corner radious on the button layer

    CGPoint saveCenter = roundedView.center;
    CGRect newFrame = CGRectMake(roundedView.frame.origin.x, roundedView.frame.origin.y, newSize, newSize);
    button.frame = newFrame;
    button.layer.cornerRadius = newSize / 2.0;
    button.center = saveCenter;
dminones
  • 2,236
  • 20
  • 21
  • thanks for the comment, i'm aware that I can make a round button (I have that already), but the question is about having an animation within the button – cwRichardKim Jan 28 '15 at 20:29
  • Sorry, I understood that you already have covered the animation and you need to mask it into the button. If you had the red and blue views as button's subviews the only thing left is to round the corners. – dminones Jan 28 '15 at 21:59
  • i'm not sure you understand. The animation isn't difficult, i need to mask it to a round button. I have the round button and i have the animation but i'm not able to mask the animation to the button – cwRichardKim Jan 28 '15 at 22:56
  • I wrote a little example to see if i undertand, check the animation on load: https://github.com/dminones/animated-button – dminones Jan 28 '15 at 23:15