让一个正方形View以四个角中的任意一角为起点放大或缩小

这篇博客介绍了一种让UIView从四个角落之一开始进行放大或缩小动画的方法。通过定义枚举类型BeginPointType指定起始点,并使用两个动画方法runAnimationChangeBigWithView和runAnimationChangeSmallWithView来分别实现放大和缩小效果。代码中利用UIView的animateWithDuration方法,结合动画选项实现了重复和自动反转的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前一直想要做这样的效果,结果昨晚无意间就弄出了大致的样子,今天对代码进行了调整,下面是最终的效果。




//缩放比例

#define kDistance 1.5


typedef enum{


    BeginPointTypeLeftTop = 0,

    BeginPointTypeLeftBottom,

    BeginPointTypeRightTop,

    BeginPointTypeRightBottom,

    

}BeginPointType;



-(void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

    

    [selfrunAnimationChangeBigWithView:self.redViewbeginPointType:BeginPointTypeLeftTop];

    [selfrunAnimationChangeBigWithView:self.orangeViewbeginPointType:BeginPointTypeRightBottom];

    [selfrunAnimationChangeBigWithView:self.blueViewbeginPointType:BeginPointTypeLeftBottom];

    [selfrunAnimationChangeBigWithView:self.greenViewbeginPointType:BeginPointTypeRightTop];

    

    [selfrunAnimationChangeSmallWithView:self.redView1beginPointType:BeginPointTypeLeftTop];

    [selfrunAnimationChangeSmallWithView:self.orangeView1beginPointType:BeginPointTypeRightBottom];

    [selfrunAnimationChangeSmallWithView:self.blueView1beginPointType:BeginPointTypeLeftBottom];

    [selfrunAnimationChangeSmallWithView:self.greenView1beginPointType:BeginPointTypeRightTop];

}


/**

 *  放大

 */

-(void)runAnimationChangeBigWithView:(UIView *)view beginPointType:(BeginPointType)beginPointType

{

    [UIViewanimateWithDuration:3.fdelay:0options:UIViewAnimationOptionRepeat |UIViewAnimationOptionAutoreverse animations:^{

        

        CGRect frame = view.frame;

        

        frame.size.width *=kDistance;

        frame.size.height *=kDistance;

        

        switch (beginPointType) {

            caseBeginPointTypeLeftBottom:

                

                frame.origin.y -= frame.size.height - frame.size.height / kDistance;

                

                break;

            caseBeginPointTypeRightTop:

                

                frame.origin.x -= frame.size.width - frame.size.width / kDistance;

                

                break;

            caseBeginPointTypeRightBottom:

                

                frame.origin.x -= frame.size.width - frame.size.width / kDistance;

                frame.origin.y -= frame.size.height - frame.size.height / kDistance;

                

                break;

            default:

                break;

        }

        

        [view setFrame:frame];

        

    } completion:nil];

}


/**

 *  缩小

 */

-(void)runAnimationChangeSmallWithView:(UIView *)view beginPointType:(BeginPointType)beginPointType

{

    [UIViewanimateWithDuration:3.fdelay:0options:UIViewAnimationOptionRepeat |UIViewAnimationOptionAutoreverse animations:^{

        

        CGRect frame = view.frame;

        

        frame.size.width /=kDistance;

        frame.size.height /=kDistance;

        

        switch (beginPointType) {

            caseBeginPointTypeLeftBottom:

                

                frame.origin.y -= frame.size.height - frame.size.height * kDistance;

                

                break;

            caseBeginPointTypeRightTop:

                

                frame.origin.x -= frame.size.width - frame.size.width * kDistance;

                

                break;

            caseBeginPointTypeRightBottom:

                

                frame.origin.x -= frame.size.width - frame.size.width * kDistance;

                frame.origin.y -= frame.size.height - frame.size.height * kDistance;

                

                break;

            default:

                break;

        }

        

        [view setFrame:frame];

        

    } completion:nil];

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值