iOS 在main storyboard 拖入一个uibutton 控件,通过代码修改其frame,但是没有变化?

//这是我创建的一个类

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

阅读 5.5k
2 个回答

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

code

找到了问题的关键,原来是auto layout的问题
图片描述

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题