两个小Points

本文解析了iOS中UIViewController的dismiss顺序及addChildViewController的使用细节。当从A控制器展示B控制器,再从B控制器展示C控制器时,B控制器调用dismiss方法会先移除最近展示的C控制器。文章还讨论了直接将视图控制器的视图添加到视图层级时需要注意的事项。

* dismissModalViewControllerAnimated *

dismiss 顺序问题:
A present B , B present C.
B 作为主体调用dismiss方法时,首先找自己作为presenter present出来的视图,将其dismiss掉。

/*
The next two methods are replacements for presentModalViewController:animated and dismissModalViewControllerAnimated: The completion handler, if provided, will be invoked after the presented controllers viewDidAppear: callback is invoked.
*/

@available(iOS 5.0, *)
public func presentViewController(viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)?)

// The completion handler, if provided, will be invoked after the dismissed controller's viewDidDisappear: callback is invoked.
@available(iOS 5.0, *)
public func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?)

* addChildViewController *

If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly. Failing to send the view controller this message will prevent any associated animation from being displayed

寻找两个最小包围圆可以采用以下思路和方法: #### 贪心算法思路 先将所有点集划分为两个子集,然后分别对这两个子集求最小包围圆。可以通过不断尝试不同的划分方式,计算每种划分下两个最小包围圆的半径之和,选择半径之和最小的划分。 以下是一个使用 Python 实现的简单示例代码,这里使用了 `scipy` 库中的 `minimize` 函数来辅助进行划分尝试: ```python import numpy as np from scipy.optimize import minimize from scipy.spatial import distance def min_enclosing_circle(points): def objective(params): center = np.array([params[0], params[1]]) radius = params[2] max_dist = np.max([distance.euclidean(center, p) for p in points]) return max_dist - radius initial_guess = [np.mean([p[0] for p in points]), np.mean([p[1] for p in points]), 0] result = minimize(objective, initial_guess) center = result.x[:2] radius = result.x[2] return center, radius def two_min_enclosing_circles(points): n = len(points) min_total_radius = float('inf') best_subset1 = None best_subset2 = None for i in range(1, 2**n - 1): subset1 = [points[j] for j in range(n) if (i >> j) & 1] subset2 = [points[j] for j in range(n) if not ((i >> j) & 1)] center1, radius1 = min_enclosing_circle(subset1) center2, radius2 = min_enclosing_circle(subset2) total_radius = radius1 + radius2 if total_radius < min_total_radius: min_total_radius = total_radius best_subset1 = subset1 best_subset2 = subset2 center1, radius1 = min_enclosing_circle(best_subset1) center2, radius2 = min_enclosing_circle(best_subset2) return (center1, radius1), (center2, radius2) # 示例点集 points = np.array([[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]) circle1, circle2 = two_min_enclosing_circles(points) print("Circle 1: Center =", circle1[0], "Radius =", circle1[1]) print("Circle 2: Center =", circle2[0], "Radius =", circle2[1]) ``` #### 基于增量算法改进 可以先求出所有点的最小包围圆,然后尝试从这个圆中移除一些点,再对剩余点和移除的点分别求最小包围圆,不断调整移除的点集,找到最优解。 ### 相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值