iOS 在main storyboard 拖入一个uibutton 控件,通过代码修改其frame,但是没有变化?
ringa_lee
ringa_lee 2017-04-17 18:00:55
[iOS讨论组]

//这是我创建的一个类

import <UIKit/UIKit.h>

IB_DESIGNABLE
@interface CircleAnimationBtn : UIButton

@property (nonatomic, assign) IBInspectable CGFloat radius;

-(void)start;
-(void)end;

@end

//.m文件=================================================

import "CircleAnimationBtn.h"

define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface CircleAnimationBtn ()
@property(nonatomic)CAShapeLayer * arcLayer;
@property(nonatomic)NSTimer * timer;
@end

@implementation CircleAnimationBtn

-(CAShapeLayer *)arcLayer
{

if (!_arcLayer) {
    _arcLayer = [CAShapeLayer layer];
    _arcLayer.lineWidth = 1.0;
    _arcLayer.strokeColor = [UIColor whiteColor].CGColor;
    _arcLayer.fillColor = [UIColor clearColor].CGColor;
    _arcLayer.strokeEnd = _arcLayer.strokeStart = 0;
    [self.layer addSublayer:_arcLayer];
    NSLog(@"%f",self.frame.size.width);
}
return _arcLayer;

}

  • (void)layoutSubviews
    {

    [super layoutSubviews];
    self.arcLayer.path = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(self.frame.size.width / 2 - _radius / 2, self.frame.size.height / 2 - _radius / 2, _radius, _radius)].CGPath;

    }

-(void)start
{

[self animationCircle];
if (_timer) {
    [_timer invalidate];
   
}
NSTimer *timer  = [NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(animationCircle) userInfo:nil repeats:YES];
_timer = timer;
[UIView animateWithDuration:0.5 animations:^{
    _arcLayer.opacity = 1;
    [self setTitle:@"?" forState:UIControlStateNormal];
    [self setTitleColor:[UIColor colorWithWhite:1.0 alpha:0] forState:UIControlStateNormal];
    NSLog(@"%f",self.center.x);
    //button其他的一些属性可以更改 但frame的大小不会发生改变
    [self setFrame:CGRectMake(SCREEN_WIDTH/2-self.frame.size.width, self.frame.origin.y, 40, 40)];
}];

}

-(void)end
{

if (_timer) {
    [_timer invalidate];
}
[_arcLayer removeAllAnimations];
[UIView animateWithDuration:0.5 animations:^{
    _arcLayer.opacity = 0;
    [self setTitleColor:[UIColor colorWithWhite:1.0 alpha:1] forState:UIControlStateNormal];
}];

}

const NSTimeInterval duration = 1.2;
-(void)animationCircle
{

CABasicAnimation * basStart = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
basStart.duration = duration;
basStart.fromValue = @0;
basStart.toValue = @1;
basStart.removedOnCompletion = YES;
[self.arcLayer addAnimation:basStart forKey:@"end"];

CABasicAnimation * basEnd = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
basEnd.beginTime = CACurrentMediaTime() + duration/2;
basEnd.fromValue = @0;
basEnd.toValue = @1;
basEnd.duration = duration/2;
basEnd.removedOnCompletion = YES;
[self.arcLayer addAnimation:basEnd forKey:@"start"];

}
@end

ringa_lee
ringa_lee

ringa_lee

全部回复(2)
高洛峰

n能不能把代码都用MarkDown拿出来啊,混在MD里根本看不了好么。。。

code
PHPz

找到了问题的关键,原来是auto layout的问题

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号