1、View 的创建与设置
- UIView 创建出来默认是透明的,在 iOS6 的时候是白色的。
复制
UIView *view = [[UIView alloc] init];
[self addSubview:view];
view.frame = CGRectMake(10, 20, 200, 100);
view.bounds = CGRectMake(0, 0, 200, 100);
view.center = self.center;
view.backgroundColor = [UIColor greenColor];
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
view.alpha = 1.0;
view.tag = 100;
view.userInteractionEnabled = YES;
view.layer.cornerRadius = 20;
view.layer.masksToBounds = YES;
view.layer.borderWidth = 5;
view.layer.borderColor = [[UIColor blueColor] CGColor];
view.clipsToBounds = NO;
NSArray *subviews = view.subviews;
UIView *superview = view.superview;
[view removeFromSuperview];
UIView *view = [self viewWithTag:100];
2、View 的层次设置
[self bringSubviewToFront:view];
[self sendSubviewToBack:view];
[self exchangeSubviewAtIndex:0 withSubviewAtIndex:2];
[self insertSubview:view1 aboveSubview:view3];
[self insertSubview:view3 belowSubview:view2];
[self insertSubview:view1 atIndex:1];
3、View 的旋转与缩放设置
-
3.1 单一形变
@property(nonatomic) CGAffineTransform transform;
self.testView.transform = CGAffineTransformMakeRotation(0.25 * M_PI);
[self.testView.layer setAffineTransform: CGAffineTransformMakeRotation(0.25 * M_PI)];
self.testView.transform = CGAffineTransformMakeScale(1, 2);
[self.testView.layer setAffineTransform: CGAffineTransformMakeScale(1, 2)];
self.testView.transform = CGAffineTransformMakeTranslation(100, 100);
[self.testView.layer setAffineTransform: CGAffineTransformMakeTranslation(100, 100)];
-
3.2 叠加形变
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(0.25 * M_PI);
self.testView.transform = CGAffineTransformScale(rotationTransform, 2, 2);
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(0.25 * M_PI);
self.testView.transform = CGAffineTransformTranslate(rotationTransform, 200, 100);
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(2, 2);
self.testView.transform = CGAffineTransformTranslate(scaleTransform, 100, 100);
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(0.25 * M_PI);
CGAffineTransform rotationScaleTransform = CGAffineTransformScale(rotationTransform, 2, 2);
self.testView.transform = CGAffineTransformTranslate(rotationScaleTransform, 200, 100);
-
3.3 累加形变
self.testView.transform = CGAffineTransformRotate(self.testView.transform, 0.25 * M_PI);
self.testView.transform = CGAffineTransformScale(self.testView.transform, 2, 2);
self.testView.transform = CGAffineTransformTranslate(self.testView.transform, 100, 100);
self.testView.transform = CGAffineTransformIdentity;
[self.testView.layer setAffineTransform:CGAffineTransformIdentity];
4、View 的跟随模式设置
fatherView.autoresizesSubviews = YES;
sonView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
5、View 的动画设置
-
-
5.1 block 方式
- 设置控件位置、尺寸、透明度等的代码,放在 animateWithDuration: block 中,将自动以动画的方式改变。
[UIView animateWithDuration:2 animations:^{
view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 60, 20, 50, 50);
} completion:^(BOOL finished) {
[UIView animateWithDuration:2 animations:^{
view.frame = CGRectMake(10, [UIScreen mainScreen].bounds.size.height - 110, 100, 100);
}];
}];
-
5.2 动画块方式
- 设置控件位置、尺寸、透明度等的代码,放在 beginAnimations: 和 commitAnimations 之间,将自动以动画的方式改变。
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelay:1.0];
[UIView setAnimationStartDate:[NSDate dateWithTimeIntervalSinceNow:10]];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationRepeatCount:CGFLOAT_MAX];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationWillStartSelector:@selector(startAnimations)];
[UIView setAnimationDidStopSelector:@selector(stopAnimations)];
view.alpha = 0.0;
view.center = CGPointMake(250, 250);
view.frame = CGRectMake(100, 180, 50, 50);
[UIView commitAnimations];
6、frame 与 NSValue 的相互转换
NSValue *freamValue = [NSValue valueWithCGRect:frame];
frame = [freamValue CGRectValue];
7、系统api详细说明
@property(class, nonatomic, readonly) Class layerClass;
@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled;
@property(nonatomic) NSInteger tag;
@property(nonatomic, readonly, strong) CALayer *layer;
@property(nonatomic, readonly) BOOL canBecomeFocused;
@property(nonatomic, readonly, getter=isFocused) BOOL focused;
@property(nonatomic, readwrite, nullable, copy) NSString *focusGroupIdentifier;
@property(nonatomic, readwrite) UIFocusGroupPriority focusGroupPriority;
@property(nonatomic, readwrite, nullable, copy) UIFocusEffect *focusEffect;
@property(nonatomic) UISemanticContentAttribute semanticContentAttribute;
@property(nonatomic, readonly) UIUserInterfaceLayoutDirection effectiveUserInterfaceLayoutDirection;
- (instancetype)initWithFrame:(CGRect)frame;
- (nullable instancetype)initWithCoder:(NSCoder *)coder;
+ (UIUserInterfaceLayoutDirection)userInterfaceLayoutDirectionForSemanticContentAttribute:(UISemanticContentAttribute)attribute;
+ (UIUserInterfaceLayoutDirection)userInterfaceLayoutDirectionForSemanticContentAttribute:(UISemanticContentAttribute)semanticContentAttribute relativeToLayoutDirection:(UIUserInterfaceLayoutDirection)layoutDirection;
if ([UIApplication.sharedApplication.preferredLanguages.firstObject containsString:@"ar"]) {
[UIView userInterfaceLayoutDirectionForSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft relativeToLayoutDirection:UIUserInterfaceLayoutDirectionLeftToRight];
}
@property(nonatomic) CGRect frame;
@property(nonatomic) CGRect bounds;
@property(nonatomic) CGPoint center;
@property(nonatomic) CGAffineTransform transform;
@property(nonatomic) CATransform3D transform3D;
@property(nonatomic) CGFloat contentScaleFactor;
@property(nonatomic, getter=isMultipleTouchEnabled) BOOL multipleTouchEnabled;
@property(nonatomic, getter=isExclusiveTouch) BOOL exclusiveTouch;
@property(nonatomic) BOOL autoresizesSubviews;
@property(nonatomic) UIViewAutoresizing autoresizingMask;
- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event;
UIView *hitTestView = [self.view hitTest:point withEvent:event];
- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event;
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
if (CGRectContainsPoint(self.bounds, point)) {
return YES;
}
return NO;
}
- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
CGPoint convertPoint = [self.view convertPoint:self.button.frame.origin toView:self.label];
- (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;
CGPoint convertPoint = [self.view convertPoint:self.label.frame.origin fromView:self.button];
- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
CGRect convertRect = [self.view convertRect:self.button.frame toView:self.label];
- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;
CGRect convertRect = [self.view convertRect:self.label.frame fromView:self.button];
- (CGSize)sizeThatFits:(CGSize)size;
CGSize size = [self.label sizeThatFits:CGSizeMake(MAXFLOAT, MAXFLOAT)];
- (void)sizeToFit;
[self.label sizeToFit];
@property(nullable, nonatomic, readonly) UIView *superview;
@property(nonatomic, readonly, copy) NSArray<__kindof UIView *> *subviews;
@property(nullable, nonatomic, readonly) UIWindow *window;
@property(nonatomic) UIEdgeInsets layoutMargins;
@property(nonatomic) NSDirectionalEdgeInsets directionalLayoutMargins;
@property(nonatomic) BOOL preservesSuperviewLayoutMargins;
@property(nonatomic) BOOL insetsLayoutMarginsFromSafeArea;
@property(nonatomic, readonly) UIEdgeInsets safeAreaInsets;
@property(readonly, strong) UILayoutGuide *layoutMarginsGuide;
@property(nonatomic, readonly, strong) UILayoutGuide *readableContentGuide;
@property(nonatomic, readonly, strong) UILayoutGuide *safeAreaLayoutGuide;
@property(nonatomic, readonly, strong) UIKeyboardLayoutGuide *keyboardLayoutGuide;
- (void)removeFromSuperview;
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;
- (void)addSubview:(UIView *)view;
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
- (void)bringSubviewToFront:(UIView *)view;
- (void)sendSubviewToBack:(UIView *)view;
- (void)didAddSubview:(UIView *)subview;
- (void)willRemoveSubview:(UIView *)subview;
- (void)willMoveToSuperview:(nullable UIView *)newSuperview;
- (void)didMoveToSuperview;
- (void)willMoveToWindow:(nullable UIWindow *)newWindow;
- (void)didMoveToWindow;
- (BOOL)isDescendantOfView:(UIView *)view;
- (nullable __kindof UIView *)viewWithTag:(NSInteger)tag;
- (void)setNeedsLayout;
- (void)layoutIfNeeded;
- (void)layoutSubviews;
- (void)layoutMarginsDidChange;
- (void)safeAreaInsetsDidChange;
@property(nonatomic) BOOL clipsToBounds;
@property(nullable, nonatomic, copy) UIColor *backgroundColor;
@property(nonatomic) CGFloat alpha;
@property(nonatomic, getter=isOpaque) BOOL opaque;
@property(nonatomic) BOOL clearsContextBeforeDrawing;
@property(nonatomic, getter=isHidden) BOOL hidden;
@property(nonatomic) UIViewContentMode contentMode;
@property(nullable, nonatomic, strong) UIView *maskView;
@property(null_resettable, nonatomic, strong) UIColor *tintColor;
@property(nonatomic) UIViewTintAdjustmentMode tintAdjustmentMode;
- (void)drawRect:(CGRect)rect;
- (void)setNeedsDisplay;
- (void)setNeedsDisplayInRect:(CGRect)rect;
- (void)tintColorDidChange;
@property(class, nonatomic, readonly) BOOL areAnimationsEnabled;
@property(class, nonatomic, readonly) NSTimeInterval inheritedAnimationDuration;
+ (void)setAnimationsEnabled:(BOOL)enabled;
+ (void)performWithoutAnimation:(void (NS_NOESCAPE ^)(void))actionsWithoutAnimation;
+ (void)animateWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^ __nullable)(BOOL finished))completion;
+ (void)animateWithDuration:(NSTimeInterval)duration
animations:(void (^)(void))animations
completion:(void (^ __nullable)(BOOL finished))completion;
+ (void)animateWithDuration:(NSTimeInterval)duration
animations:(void (^)(void))animations;
+ (void)animateWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
usingSpringWithDamping:(CGFloat)dampingRatio
initialSpringVelocity:(CGFloat)velocity
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^ __nullable)(BOOL finished))completion;
+ (void)transitionWithView:(UIView *)view
duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options
animations:(void (^ __nullable)(void))animations
completion:(void (^ __nullable)(BOOL finished))completion;
+ (void)transitionFromView:(UIView *)fromView
toView:(UIView *)toView
duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options
completion:(void (^ __nullable)(BOOL finished))completion;
+ (void)performSystemAnimation:(UISystemAnimation)animation
onViews:(NSArray<__kindof UIView *> *)views
options:(UIViewAnimationOptions)options
animations:(void (^ __nullable)(void))parallelAnimations
completion:(void (^ __nullable)(BOOL finished))completion;
+ (void)modifyAnimationsWithRepeatCount:(CGFloat)count
autoreverses:(BOOL)autoreverses
animations:(void(NS_NOESCAPE ^)(void))animations;
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
options:(UIViewKeyframeAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^ __nullable)(BOOL finished))completion;
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime
relativeDuration:(double)frameDuration
animations:(void (^)(void))animations;
@property(nullable, nonatomic, copy) NSArray<__kindof UIGestureRecognizer *> *gestureRecognizers;
- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer;
- (void)removeGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer;
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
@property (copy, nonatomic) NSArray<__kindof UIMotionEffect *> *motionEffects;
- (void)addMotionEffect:(UIMotionEffect *)effect;
UIInterpolatingMotionEffect *motionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"layer.transform" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
motionEffect.minimumRelativeValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-0.1, 0, 1, 0)];
motionEffect.maximumRelativeValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0.1, 0, 1, 0)];
[imageView addMotionEffect:motionEffect];
- (void)removeMotionEffect:(UIMotionEffect *)effect;
UIInterpolatingMotionEffect *motionEffect = ;
[imageView removeMotionEffect:motionEffect];
@interface UIView (UIConstraintBasedLayoutCoreMethods)
- (void)updateConstraintsIfNeeded;
- (void)updateConstraints NS_REQUIRES_SUPER;
- (BOOL)needsUpdateConstraints;
- (void)setNeedsUpdateConstraints;
@end
@interface UIView (UIConstraintBasedCompatibility)
@property(nonatomic) BOOL translatesAutoresizingMaskIntoConstraints;
@property(class, nonatomic, readonly) BOOL requiresConstraintBasedLayout;
@end
@interface UIView (UIConstraintBasedLayoutLayering)
- (CGRect)alignmentRectForFrame:(CGRect)frame;
- (CGRect)frameForAlignmentRect:(CGRect)alignmentRect;
@property(nonatomic, readonly) UIEdgeInsets alignmentRectInsets;
@property(readonly, strong) UIView *viewForFirstBaselineLayout;
@property(readonly, strong) UIView *viewForLastBaselineLayout;
UIKIT_EXTERN const CGFloat UIViewNoIntrinsicMetric;
@property(nonatomic, readonly) CGSize intrinsicContentSize;
- (void)invalidateIntrinsicContentSize;
- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis;
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis;
- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis;
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis;
@end
@interface UIView (UIConstraintBasedLayoutFittingSize)
- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize;
- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority;
@end
@interface UIView (UILayoutGuideSupport)
@property(nonatomic, readonly, copy) NSArray<__kindof UILayoutGuide *> *layoutGuides;
- (void)addLayoutGuide:(UILayoutGuide *)layoutGuide;
- (void)removeLayoutGuide:(UILayoutGuide *)layoutGuide;
@end
@class NSLayoutXAxisAnchor, NSLayoutYAxisAnchor, NSLayoutDimension;
@interface UIView (UIViewLayoutConstraintCreation)
@property(nonatomic, readonly, strong) NSLayoutXAxisAnchor *leadingAnchor;
@property(nonatomic, readonly, strong) NSLayoutXAxisAnchor *trailingAnchor;
@property(nonatomic, readonly, strong) NSLayoutXAxisAnchor *leftAnchor;
@property(nonatomic, readonly, strong) NSLayoutXAxisAnchor *rightAnchor;
@property(nonatomic, readonly, strong) NSLayoutYAxisAnchor *topAnchor;
@property(nonatomic, readonly, strong) NSLayoutYAxisAnchor *bottomAnchor;
@property(nonatomic, readonly, strong) NSLayoutDimension *widthAnchor;
@property(nonatomic, readonly, strong) NSLayoutDimension *heightAnchor;
@property(nonatomic, readonly, strong) NSLayoutXAxisAnchor *centerXAnchor;
@property(nonatomic, readonly, strong) NSLayoutYAxisAnchor *centerYAnchor;
@property(nonatomic, readonly, strong) NSLayoutYAxisAnchor *firstBaselineAnchor;
@property(nonatomic, readonly, strong) NSLayoutYAxisAnchor *lastBaselineAnchor;
@end
@interface UIView (UIConstraintBasedLayoutDebugging)
- (NSArray<__kindof NSLayoutConstraint *> *)constraintsAffectingLayoutForAxis:(UILayoutConstraintAxis)axis;
@property(nonatomic, readonly) BOOL hasAmbiguousLayout;
- (void)exerciseAmbiguityInLayout;
@end
@interface UILayoutGuide (UIConstraintBasedLayoutDebugging)
- (NSArray<__kindof NSLayoutConstraint *> *)constraintsAffectingLayoutForAxis:(UILayoutConstraintAxis)axis;
@property(nonatomic, readonly) BOOL hasAmbiguousLayout;
@end
@interface UIView (UIStateRestoration)
@property (nullable, nonatomic, copy) NSString *restorationIdentifier;
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder;
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder;
@end
@interface UIView (UISnapshotting)
- (nullable UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates;
- (nullable UIView *)resizableSnapshotViewFromRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates withCapInsets:(UIEdgeInsets)capInsets;
- (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates;
@end
@interface UIView (UserInterfaceStyle)
@property(nonatomic) UIUserInterfaceStyle overrideUserInterfaceStyle;
@end
@interface UIView (UIContentSizeCategoryLimit)
@property(nonatomic, copy, nullable) UIContentSizeCategory minimumContentSizeCategory;
@property(nonatomic, copy, nullable) UIContentSizeCategory maximumContentSizeCategory;
@property(nonatomic, copy, readonly) NSString *appliedContentSizeCategoryLimitsDescription;
@end
8、其他问题
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)