IOS----Square block animation button implementation

Source: Internet
Author: User

Suddenly remember the last time the interviewer asked a question: How to create a diamond, and let it corresponding click the event, you can open and close the effect. The first reaction was to use button to fill the picture of the diamond, but the examiner said that the area of the click is not sensitive, after all, the button is rectangular. At that time was indeed questioned by the examiner, and later thought up, found so!

You may have seen this function: SetTransform: Yes, the principle of our implementation is to rotate the square button 45 degrees to become a rectangle. Combining precise coordinate calculation, the UIView animation is adopted to realize the opening and closing effect. Isn't it simple?

If you do not want to use the button with ImageView, join gesture recognition to click the event accordingly. Post code:

    [_myimage settransform:cgaffinetransformmakerotation (M_pi_4)];    [_myimage2 settransform:cgaffinetransformmakerotation (M_pi_4)];    [_myimage3 settransform:cgaffinetransformmakerotation (M_pi_4)];    [_myimage4 settransform:cgaffinetransformmakerotation (M_pi_4)];

The above is the four image that will be declared rotated 45 degrees. Next is the animation:

    [UIView animatewithduration:0.7 animations:^{        //move picture        _myimage.center = Cgpointmake (_myimage.center.x, _ Myimage.center.y-length);        _myimage2.center = Cgpointmake (_myimage2.center.x-length, _myimage2.center.y);        _myimage3.center = Cgpointmake (_myimage3.center.x+length, _myimage3.center.y);        _myimage4.center = Cgpointmake (_myimage4.center.x, _myimage4.center.y+length);        length =-length;    }]

It is convenient to move the center of each picture. Here I have achieved four different animation effects, see the code for details.

The final effect is as follows:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.